Javascript: String.match() - 在正则表达式中传递字符串变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31043039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:10:12  来源:igfitidea点击:

Javascript: String.match() - pass string variable in regular expression

javascriptstring

提问by Mohitt

I tried to rewrite the method (part of tutorialon w3schools).

我试图重写该方法(w3schools教程的一部分)。

The problem is to make a variable string to become part of the regular expression.

问题是让一个变量字符串成为正则表达式的一部分。

Tutorial Sample code:

教程示例代码:

function myFunction() {
    var str = "The rain in SPAIN stays mainly in the plain"; 
    var res = str.match(/ain/gi);
    console.log(res)
}

I tried:

我试过:

function myFunction() {
    var str = "The rain in SPAIN stays mainly in the plain"; 
    var test = "ain";
    var re = "/"+test+"/gi";
    var res = str.match(re);
    console.log(res);
}

The way I tried did not work.

我尝试的方法不起作用。

回答by CD..

Use the regex constructor, like:

使用正则表达式构造函数,例如:

function myFunction() {
    var str = "The rain in SPAIN stays mainly in the plain",
        test = "ain",
        re = new RegExp(test, 'gi'),
        res = str.match(re);

    console.log(res);
}

回答by Avinash Raj

You need to use RegExpconstructor if you want to pass a value of variable as regex.

RegExp如果要将变量值作为正则表达式传递,则需要使用构造函数。

var test = "ain";
var re = new RegExp(test, "gi");

If your variable contains special chars, it's better to escape those.

如果您的变量包含特殊字符,最好将其转义。

var re = new RegExp(test.replace(/(\W)/g, "\"), "gi");

回答by squiroid

Yes match()not works with string literals

Yesmatch()不适用于字符串文字

You can use var re = new RegExp(test,"gi");

您可以使用 var re = new RegExp(test,"gi");

 var str = "The rain in SPAIN stays mainly in the plain"; 
    var test = "ain";
    var re = new RegExp(test,"gi");
    var res = str.match(re);
    console.log(res);

回答by eX0du5

You could have searched for "dynamic regular expressions" and you would have found: Javascript Regexp dynamic generation from variables?or Use dynamic (variable) string as regex pattern in JavaScriptwhich both describe it very well.

您可以搜索“动态正则表达式”,然后您会发现: Javascript Regexp dynamic generation from variables? 在 JavaScript 中使用动态(可变)字符串作为正则表达式模式,这两者都很好地描述了它。

回答by TrapII

Note that you pass a string to match. If we follow the documentation, this will do a new RegExp(test). So you should avoid /and /gistrings and add corresponding flags to the RegExp constructor: the default constructor doesn't add neither global search (g) nor case insensitive search (i).

请注意,您将字符串传递给match. 如果我们遵循文档,这将执行new RegExp(test). 所以你应该避免//gi字符串,并在 RegExp 构造函数中添加相应的标志:默认构造函数既不添加全局搜索 ( g) 也不添加不区分大小写的搜索 ( i)。

So the solution to your problem:

所以你的问题的解决方案:

var str = "The rain in SPAIN stays mainly in the plain"; 
var test = "ain";
var res = str.match(new RegExp(test, "gi"));

This will returns :

这将返回:

Array [ "ain", "AIN", "ain", "ain" ]

Note :

笔记 :

The form str.match(test, "gi");works only in Firefox browser but is deprecated and throws a console warning from Firefox 39 (see RGraham comment).

该表单str.match(test, "gi");仅适用于 Firefox 浏览器,但已被弃用并从 Firefox 39 中引发控制台警告(请参阅 RGraham 评论)。