我是不是很密集,或者边界似乎破坏了 JavaScript 正则表达式中连字符的匹配?
var string1 = "example words";
/\bexample\b/.test(string1); // true (all good)
var string2 = "-example -words";
/\b-example\b/.test(string2); // false (confusion)
也许我误解了边界行为?
转义连字符似乎也无济于事......
最佳答案
这是因为\b
是单词边界,-
或连字符已经被认为是非单词字符。您可以改用此正则表达式:
/-example\b/.test('-example -words');
true
关于javascript - 使用边界时,带有连字符的正则表达式似乎被破坏了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23786144/