Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

3 of 8
insert duplicate link

passing variable to a regexp in javascript

Possible Duplicate:
Escape string for use in Javascript regex

I have a msg like

Max {0} chars allowed in {1}

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 its not able to take the param "i" to create the reg exp.

Whats the way to achieve the same ?

Nrj
  • 6.9k
  • 7
  • 49
  • 59
lang-js

AltStyle によって変換されたページ (->オリジナル) /