8
我要检查一个正则表达式,如果一个字符串包含特定单词的JavaScript正则表达式测试。它按预期工作:如果字符串包含特定单词(可变)
/\bword\b/.test('a long text with the desired word amongst others'); // true
/\bamong\b/.test('a long text with the desired word amongst others'); // false
但我需要在变量中检查的字。使用new RegExp
不能正常工作,它总是返回false
:
var myString = 'a long text with the desired word amongst others';
var myWord = 'word';
new RegExp('\b' + myWord + '\b').test(myString); // false
myWord = "among";
new RegExp('\b' + myWord + '\b').test(myString); // false
这里有什么问题?
+1,看到http://jsfiddle.net/TxQ7c/6/ – 2012-04-10 18:06:54
你发布你的答案,同时想通了。谢谢。不知道是什么有这个答案要做到: – Alp 2012-04-10 18:10:55
\ B就不会与所有的语言 – Uri 2016-10-21 17:17:10