【发布时间】:2012-04-10 17:59:55
【问题描述】:
我有一个正则表达式来检查字符串是否包含特定单词。它按预期工作:
/\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
这里有什么问题?
【问题讨论】:
标签: javascript regex string-comparison