0

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');
1
  • It is traditional to upvote answers. Commented Aug 6, 2010 at 10:37

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

0
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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.