Passing regex modifier options to RegExp object

JavascriptRegexString

Javascript Problem Overview


I am trying to create something similar to this:

var regexp_loc = /e/i;

except I want the regexp to be dependent on a string, so I tried to use new RegExp but I couldn't get what i wanted.

Basically I want the e in the above regexp to be a string variable but I fail with the syntax.

I tried something like this:

var keyword = "something";

var test_regexp = new RegExp("/" + keyword + "/i");

Basically I want to search for a sub string in a larger string then replace the string with some other string, case insensitive.

regards, alexander

Javascript Solutions


Solution 1 - Javascript

You need to pass the second parameter:

var r = new RegExp(keyword, "i");

You will also need to escape any special characters in the string to prevent regex injection attacks.

Solution 2 - Javascript

You should also remember to watch out for escape characters within a string...

For example if you wished to detect for a single number \d{1} and you did this...

var pattern = "\d{1}";
var re = new RegExp(pattern);

re.exec("1"); // fail! :(

that would fail as the initial \ is an escape character, you would need to "escape the escape", like so...

var pattern = "\\d{1}" // <-- spot the extra '\'
var re = new RegExp(pattern);

re.exec("1"); // success! :D

Solution 3 - Javascript

When using the RegExp constructor, you don't need the slashes like you do when using a regexp literal. So:

new RegExp(keyword, "i");

Note that you pass in the flags in the second parameter. See here for more info.

Solution 4 - Javascript

Want to share an example here:

I want to replace a string like: hi[var1][var2] to hi[newVar][var2]. and var1 are dynamic generated in the page.

so I had to use:

var regex = new RegExp("\\\\["+var1+"\\\\]",'ig');
mystring.replace(regex,'[newVar]');

This works pretty good to me. in case anyone need this like me. The reason I have to go with [] is var1 might be a very easy pattern itself, adding the [] would be much accurate.

Solution 5 - Javascript

var keyword = "something";

var test_regexp = new RegExp(something,"i");

Solution 6 - Javascript

You need to convert RegExp, you actually can create a simple function to do it for you:

function toReg(str) {
  if(!str || typeof str !== "string") {
    return;
  }
  return new RegExp(str, "i");
}

and call it like:

toReg("something")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexanderView Question on Stackoverflow
Solution 1 - JavascriptSLaksView Answer on Stackoverflow
Solution 2 - JavascriptRicView Answer on Stackoverflow
Solution 3 - JavascriptJames SulakView Answer on Stackoverflow
Solution 4 - JavascriptliudaxingtxView Answer on Stackoverflow
Solution 5 - JavascriptHarshal KhatriView Answer on Stackoverflow
Solution 6 - JavascriptAlirezaView Answer on Stackoverflow