Ignore case while using RegExp() constructor to create a regex of a variable

I am trying to create a regex that will test a string that has been passed as an argument into a function. I’ve successfully done so using …

let x = new RegExp(strArg);

So now x is my new regex, and I can test my string, however, I’d like to ignore case in the regex, and I can’t figure out how to do so. It seems like it should be simple, but I can’t figure it out, and I’m stuck. I’ve been searching the forum and google for info on this issue, but I can’t find anything. Can anyone help point me in the right direction?

https://devdocs.io/javascript/global_objects/regexp has some great ideas on how to include flags in your regex. Look at the Literal notation and constructor section, it includes exactly the case you’re after.

1 Like