57

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

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Alexander
  • 2,423
  • 4
  • 23
  • 17

6 Answers6

78

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Thanks, thought I needed the opening and closing slashes. (>_<) – Alex K Mar 12 '13 at 20:28
  • 4
    How do I pass multiple flags like i and g? I have been trying this since long time now. I tried "ig" but only g works. I tried "i,g" but it is invalid. I tried ["i", "g"] but it is invalid. How? – Tarun Dec 22 '16 at 13:26
  • 1
    what about if you need to add ^ before the expression. This answer could use some love – Xitcod13 Jan 17 '19 at 19:42
47

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
Ric
  • 640
  • 5
  • 10
  • 1
    I was bit by the double escape when using a string to create a pattern. I am glad I found this. – Pete Mar 08 '16 at 21:04
  • One more thing that tripped me up, the string version of the pattern that gets fed to the RegExp object won't work if you start and end with "/" . A regular expression in js might be var pattern = /\d{1}/ , no quotations needed, but once you convert to string, it would become var patternString = "\\d{1}" , like Ric has it in his example. – 1mike12 Jun 09 '16 at 01:16
  • Hours trying to get that RegExp working,and finally a awesome answer. The error was that I was forgetting \\ in the beginning, I was using only one. – Fusseldieb Sep 19 '16 at 01:45
11

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.

James Sulak
  • 31,389
  • 11
  • 53
  • 57
1

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.

br3t
  • 1,646
  • 2
  • 20
  • 27
liudaxingtx
  • 107
  • 10
0

var keyword = "something";

var test_regexp = new RegExp(something,"i");
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
0

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")
Alireza
  • 100,211
  • 27
  • 269
  • 172