Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何匹配正则表达式中的特殊符号?
– is not the same as -; – is longer and seems to have a different character code
我没想过测试特殊字符。
我需要检查的示例字符串:
Testshop – Best fan ware | Example shop
应该返回
Testshop
我使用的正则表达式:
/[^\|\-\;\–]*/
但是它不会返回正确的结果。问题是 - 字符。
\除了-(破折号)之外是不必要的。
\
-
>> 'Testshop – Best fan ware | Example shop'[/[^|\-;–]*/] => "Testshop "
如果您只想要字母数字字符,请使用\w+(也匹配_):
\w+
_
>> 'Testshop – Best fan ware | Example shop'[/\w+/] => "Testshop"