开发者

How do I pass function parameters into a regex query?

开发者 https://www.devze.com 2023-01-10 23:26 出处:网络
开发者_高级运维How would I go about passing function parameters into a regex query? Many thanks.
开发者_高级运维

How would I go about passing function parameters into a regex query? Many thanks.

function match(str, arg1, arg2){
   var result = str.match(/(arg1 | arg2)/m);
   log(result) //null
}

match('claire nick steve', 'nick','steve');


You need to pass a normal string to the Regex constructor, like this:

var result = str.match(new Regex("(" + arg1 + "|" + arg2 + ")", "m");

If you use backslashes in the regex, you'll need to escape them (\\) since it's normal string literal.


http://www.regular-expressions.info/javascript.html

you are using a literal, try initializing the object with new RegExp("your string");


function match(str, arg1, arg2){
   var re=new RegExp("(" + arg1 + "|" + arg2 +")","m");
   var result = str.match(re);
   log(result) //null
}

match('claire nick steve', 'nick','steve');
0

精彩评论

暂无评论...
验证码 换一张
取 消