Question

I am trying to do validations by matching inputs to regexes in a case insensitive way. The regex comes down from service as a string on an object. I might get something like:

{regex:"ane"}

I can do the following:

var rx = new RegExp(object.regex);  /*The regex is now: /ane/*/
"plane".match(rx);

However, I what I really want to do is the following:

var rxInsensitive = new RegExp(/ane/i);  /*The regex is now: /ane/i */
"plANE".match(rx);

I am having problems converting the string to this form however. When I do the following:

var rxInsensitive = newRegExp(object.regex + "/i");

I end up getting the regex /ane/i/ instead of /ane/i. Does anyone have any suggestions?

Was it helpful?

Solution

var re = new RegExp("pattern", "flags");

so it would be

var rx = new RegExp(object.regex,"i");

See MDN for more info

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top