Reputation: 3200
IE8 is reporting an Unexpected Quantifier error, with the following JS function, which I want to use to strip certain symbols from a string.
function stripCurrencySymbols(currStr){
var symbols = [",",'£',"p","$"];
for(i in symbols){
currStr = currStr.replace(new RegExp(symbols[i], 'g'),"");
}
return currStr;
}
I think it's because the $ needs to be escaped, I tried replacing it with \$, but to no avail. Any ideas?
Upvotes: 2
Views: 1126
Reputation: 336378
It should be "\\$"
when you're constructing a regex from a string.
Also, why not do this:
function stripCurrencySymbols(currStr){
return currStr.replace(/[,£p$]/g, "");
}
[,£p$]
is a character class meaning "one of the included characters"; inside a character class, most metacharacters like $
need not be escaped.
Upvotes: 3