44

Possible Duplicate:
Escape string for use in Javascript regex

I have a msg like this:

Max {0} chars allowed in {1}

And I have a function to create a message using the arguments passed as

for(var i = 0; i < agrs.length; i++){
    reg = new RegExp('\{'+i+'\}', 'gi');
    key = key.replace(reg,agrs[i])
}

The problem is that it's not able to take the param i to create the reg exp.

What's the way to achieve this?

Community
  • 1
  • 1
Nrj
  • 6,723
  • 7
  • 46
  • 58
  • 3
    @casperOne, what is the duplicate? Neither [this question](http://stackoverflow.com/questions/4029109/javascript-regex-how-to-put-a-variable-inside-a-regular-expression) nor [this question](http://stackoverflow.com/questions/494035/how-do-you-pass-a-variable-to-a-regular-expression-javascript) were the same as this question. This question is important because it shows actually mixing a string variable into a regex pattern, whereas the other questions' answers only provided examples where escaping characters was not an issue. – MandM Dec 04 '15 at 13:08
  • I agree with MandM. And given that this question has been marked by duplicate we cannot update the answers with ES6 JS, and people will keep using verbose JS. @casperOne can you unmark this as duplicate? – Daniel Reina Aug 13 '18 at 14:10

4 Answers4

58

Your regexp is /{0}/gi since you create it from a string. And it is not a valid expression. You need to escape { in the regexp because it has a special meaning in the regexp syntax, so it should be:

new RegExp('\\{'+i+'\\}', 'gi');

which is /\\{0\\}/gi. You need to escape the escaping \\ in the string.

Community
  • 1
  • 1
artificialidiot
  • 5,309
  • 29
  • 27
6

I would strongly encourage you to use the functional form of String.replace() to solve your problem, rather than trying to parametrize the regexp in a for-loop that iterates over {0},{1},etc.

In other words, rather than look for '{0}' or '{1}', just look for '{([0-9]+)}' (i.e. {}'s surrounding an arbitrary number, and pass a function to the replace() function to intelligently replace these expressions based on the number between the {}'s. This lets you use a RegExp literal which you can write as /{([0-9]+)}/ rather than mess around with escape characters in strings.

Something like this:

s='the song says {0} little {1} little {2} little-endians';
args=['zero','one','two'];
new_string = s.replace(/{([0-9]+)}/g, function(wholematch,firstmatch)
  {return args[(+firstmatch)]; }
);

which yields

the song says zero little one little two little-endians

See this similar question.

edit: if you want to leave alone items that are greater than the length of your args list, make sure to sanity-check the parameter number:

s='the song says {0} little {1} little {2} little-endians,\n'+
  ' {3} little {4} little {5} little-endians';
args=['zero','one','two'];
new_string = s.replace(/{([0-9]+)}/g, function(wholematch,firstmatch)
  {var i = +firstmatch; return i < args.length ? args[i] : wholematch;}
);

which yields

the song says zero little one little two little-endians,
 {3} little {4} little {5} little-endians
Community
  • 1
  • 1
Jason S
  • 184,598
  • 164
  • 608
  • 970
3

I think your second line is supposed to be

reg = new RegExp('\{'+i+'\}', 'gi');

but I don't know if that's your only problem or not.

Gareth
  • 133,157
  • 36
  • 148
  • 157
  • Yep it is like that, but it does not allow to create one. Says "invalid quantifier". – Nrj Jan 28 '09 at 12:57
1
function escapeRegExp(str) {
  return str.replace(/[-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

var re = new RegExp(escapeRegExp(str));   

See: Escape string for use in Javascript regex

Community
  • 1
  • 1
coolaj86
  • 74,004
  • 20
  • 105
  • 125