Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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_Regex_Match_Web Deployment - Fatal编程技术网

Javascript 将参数传递到函数匹配中

Javascript 将参数传递到函数匹配中,javascript,regex,match,web-deployment,Javascript,Regex,Match,Web Deployment,我在搜索引擎中使用match函数,因此每当用户键入搜索字符串时,我都会使用该字符串并在包含国家名称的数组中使用match函数,但它似乎不起作用 例如,如果我这样做: var string = "algeria"; var res = string.match(/alge/g); //alge is what the user would have typed in the search bar alert(res); 我得到一个字符串res=“alge”://从而验证阿尔及利亚是否存在alge

我在搜索引擎中使用match函数,因此每当用户键入搜索字符串时,我都会使用该字符串并在包含国家名称的数组中使用match函数,但它似乎不起作用

例如,如果我这样做:

var string = "algeria";
var res = string.match(/alge/g); //alge is what the user would have typed in the search bar
alert(res);
我得到一个字符串
res=“alge”
://从而验证阿尔及利亚是否存在alge

但如果我这样做,它会返回null,为什么?我怎样才能让它工作呢

var regex = "/alge/g";
var string = "algeria";
var res = string.match(regex);
alert(res);

要从字符串生成正则表达式,需要创建正则表达式对象:

var regex = new RegExp("alge", "g");

(请注意,除非您的用户将键入实际的正则表达式,否则您将需要转义正则表达式中具有特殊含义的任何字符-有关方法,请参阅。)

要从字符串生成正则表达式,您需要创建一个RegExp对象:

var regex = new RegExp("alge", "g");

(请注意,除非您的用户将键入实际的正则表达式,否则您将需要转义正则表达式中具有特殊含义的任何字符-有关方法,请参阅。)

要从字符串生成正则表达式,您需要创建一个RegExp对象:

var regex = new RegExp("alge", "g");

(请注意,除非您的用户将键入实际的正则表达式,否则您将需要转义正则表达式中具有特殊含义的任何字符-有关方法,请参阅。)

要从字符串生成正则表达式,您需要创建一个RegExp对象:

var regex = new RegExp("alge", "g");

(请注意,除非您的用户将键入实际的正则表达式,否则您将需要转义正则表达式中具有特殊含义的任何字符-有关方法,请参阅。)

您不需要在正则表达式周围加引号:

var regex = /alge/g;

您不需要在正则表达式周围加引号:

var regex = /alge/g;

您不需要在正则表达式周围加引号:

var regex = /alge/g;

您不需要在正则表达式周围加引号:

var regex = /alge/g;

删除正则表达式周围的引号

              var regex = /alge/g;
              var string = "algeria";
              var res = string.match(regex);
              alert(res);

删除正则表达式周围的引号

              var regex = /alge/g;
              var string = "algeria";
              var res = string.match(regex);
              alert(res);

删除正则表达式周围的引号

              var regex = /alge/g;
              var string = "algeria";
              var res = string.match(regex);
              alert(res);

删除正则表达式周围的引号

              var regex = /alge/g;
              var string = "algeria";
              var res = string.match(regex);
              alert(res);

找到答案后,match函数接受一个regex对象

             var regex = new RegExp(string, "g");
            var res = text.match(regex);

这很好用

找到答案,match函数接受一个regex对象,所以必须这样做

             var regex = new RegExp(string, "g");
            var res = text.match(regex);

这很好用

找到答案,match函数接受一个regex对象,所以必须这样做

             var regex = new RegExp(string, "g");
            var res = text.match(regex);

这很好用

找到答案,match函数接受一个regex对象,所以必须这样做

             var regex = new RegExp(string, "g");
            var res = text.match(regex);

这很好

只是输入了相同的内容。在原始的post-var-regex=“/alge/g”是一个字符串,而不是正则表达式:)只是键入了相同的内容。在原始的post-var-regex=“/alge/g”是一个字符串,而不是正则表达式:)只是键入了相同的内容。在原始的post-var-regex=“/alge/g”是一个字符串,而不是正则表达式:)只是键入了相同的内容。在原始的post变量regex=“/alge/g”是一个字符串,而不是正则表达式:)