So, my program can accept strings like:
@vS050_A1_0002「Hello!」
@v2124_A4_005「Bye, Bye」
@k789_S3_100「Lorem imsum」
and also a string without a version number:
Vivamus blandit et nibh nec placerat.
In hac habitasse platea dictumst. Suspendisse potenti.
I tried to write a regular expression to remove the version from the string. And it came out something like this: ^[^「]*
. It finds a match for the version and successfully replaces it with an empty string. But the problem is that it also works on strings without a version. That is, it deletes the entire line without the version.
So, my question is, how do I write a regular expression that only works with sentences that start with a version?
Example: A program receives 2 strings. The first @vS050_A1_0002「Hello!」
and the second Example2 string2
. The result after the regular expression should be 「Hello!」
and Example2 string2
.
CodePudding user response:
/^@\w /
will match @
followed by letters and numbers at the beginning of the string.
const strings = [
'@vS050_A1_0002「Hello! ',
'@v2124_A4_005「Bye, Bye」',
'@k789_S3_100「Lorem imsum」',
'Vivamus blandit et nibh nec placerat.',
'In hac habitasse platea dictumst. Suspendisse potenti.'
];
const reg = /^@\w \s*/;
strings.forEach(s => console.log(s.replace(reg, '')));