以下代码按预期写入“响应”:
var str = "Would you like to have responses";
var pattern = "Response";
document.write(str.match(new RegExp(pattern,'gi')));
但是当我添加一个说明符时\b
,下面会写“null”:
var str = "Would you like to have responses";
var pattern = "\bResponse";
document.write(str.match(new RegExp(pattern,'gi')));
我怎样才能让它工作?
有趣的是,以下两个作品
document.write(str.match(new RegExp(/Response/gi)));
document.write(str.match(new RegExp(/\bResponse/gi)));
但我想使用new RegExp(pattern,modifiers);
语法。