4

I have a string:

var _codes = "1234,1414,5555,3333,2222,5566,4545";
var regex = new RegExp(/1234/i);
var _found = _codes.search(regex);

//this works sofar. 

nowi want to do it with variable:

like this:

var id = "1234";
regex = new RegExp("\\"+id+"\\/i");

but it doesn't work. any ideas?

Thanks!

1 Answer 1

4

When using the RegExp constructor, you don't supply delimiters and the flags go in the second argument.

var id = "1234";
regex = new RegExp(id, "i");

However, the RegExp just for 1234 with i doesn't really make sense. Use indexOf() instead.

However, perhaps you really did mean to match numbers surrounded with a \. In that case, leave them in there.

2
  • And the i flag (case insensitive) doesn't make much sense when the expression is only digits.
    – RobG
    May 27, 2011 at 2:41
  • @RobG Yep, I mentioned that above :)
    – alex
    May 27, 2011 at 3:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.