I can’t understand why I can’t use a variable for the regex string. See link: http://jsfiddle.net/nmWuw/1/
It works without the variable but not when a variable is used for the regex. I escaped my backslashes as well. Output should be ‘1,234,567,890’.
That is a string. It is not a regexp object and a replacement string. When it is passed to
replace, it is one parameter, not two. You need to pass both parameters separately:http://jsfiddle.net/nmWuw/3/
Note that you could define them in one call, if you really wanted to, using an array and
apply:http://jsfiddle.net/nmWuw/6/
This is hardly a good idea, however — it’s much harder to read and much less intuitive.