3

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?

1 Answer 1

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

so it would be

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

See MDN for more info

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

2 Comments

Wow, just what I needed, thank you for the quick response. I will accept when it becomes available.
I wonder how can we do the same using regex literal notation instead of constructor notation e.g. matching or 2 label with case in-sensitiveness (label1|label2) ?

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.