字符串的RegEx,该字符串的开头可能有版本

0qx6xfy6  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(138)

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 .

bfnvny8b

bfnvny8b1#

/^@\w+/

将匹配字符串开头的@后跟字母和数字。

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, '')));

相关问题