Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.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
动态创建regex-Javascript_Javascript_Regex - Fatal编程技术网

动态创建regex-Javascript

动态创建regex-Javascript,javascript,regex,Javascript,Regex,我在数组中有一些停止词,需要从给定的文本中省略。假设停止字是[a,an,the,is,the,of,and,or,to,of,as,for],正则表达式将是/\b(?:a | an | the | is | of | and | or | to | of | as | for)\b/ig 停止词可能会有所不同。因此,我尝试使用RegExp,如下所示 var s='a | an | the | is | of | and | or | to | of | as | for' var reg=new

我在数组中有一些停止词,需要从给定的文本中省略。假设停止字是[a,an,the,is,the,of,and,or,to,of,as,for],正则表达式将是
/\b(?:a | an | the | is | of | and | or | to | of | as | for)\b/ig

停止词可能会有所不同。因此,我尝试使用
RegExp
,如下所示

var s='a | an | the | is | of | and | or | to | of | as | for'
var reg=new RegExp('/\b(?:'+s+')\b/ig','i')

但我遵循的上述方法不起作用。我哪里做错了


下面是一些代码:

当您使用
rexep
构造函数时,您不包括
/
分隔符,修饰符放在单独的参数中:

var reg = new RegExp('\\b(?:' + s + ')\\b','ig')

您还需要转义反斜杠,因为它们也是字符串转义前缀。

当您使用
RexExp
构造函数时,您不包括
/
分隔符,修饰符位于单独的参数中:

var reg = new RegExp('\\b(?:' + s + ')\\b','ig')

您还需要转义反斜杠,因为它们也是字符串转义前缀。

并且还转义字符串中的反斜杠literals@DenysS谢谢你,修正了这个问题。还可以用绳子逃脱反睫毛literals@DenysS谢谢,修好了。