Alex Rivera | Logout

passing variable to a regexp in javascript

Asked 2009-01-28T12:52:16.050
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?

Edit
Report

1 Answer

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.

answered 2009-01-28T12:54:34.533

Your Answer