Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript正则表达式字符串模式_Javascript - Fatal编程技术网

Javascript正则表达式字符串模式

Javascript正则表达式字符串模式,javascript,Javascript,我需要检查动态生成的字符串的JS匹配 即 for(i=0;i

我需要检查动态生成的字符串的JS匹配

for(i=0;i
但是,这段代码不起作用-我想是因为引用了。我该怎么做


非常感谢。

文本只起到文本的作用。不在一个字符串内

如果要使用字符串模式创建正则表达式,则需要创建新的RegExp对象:

var re = new RegExp(pattern_1)
在这种情况下,您将省略封闭的前斜杠(
/
)。这两条线是等效的:

var re = /abc/g;
var re = new RegExp("abc", "g");

问题是您正在将字符串传递给
search
函数,因此它将其视为字符串。尝试使用如下所示的RegExp对象:

myregexp = new RegExp("part of " + arr[i] + " string!", "i")
if (string.search(myregexp) != -1) {
   arr_num[i]++;
}
试试这个:

// note you dont need those "/" and that "i" modifier is sent as second argument
pattern_1= new RegExp("part of "+arr[i]+" string!", "i");

你想配什么?但是您是正确的,如果您需要动态创建regex,则需要使用new
RegExp(“string”)
// note you dont need those "/" and that "i" modifier is sent as second argument
pattern_1= new RegExp("part of "+arr[i]+" string!", "i");