I want to delete all characters like [ or ] or & in a string i.E. :
“[foo] & bar” -> “foo bar”
I don’t want to call replace 3 times, is there an easier way than just coding:
var s="[foo] & bar";
s=s.replace('[','');
s=s.replace(']','');
s=s.replace('&','');
Regular expressions [xkcd] (I feel like him ;)):
Reference:
string.replaceSide note:
JavaScript’s
replacefunction only replaces the first occurrence of a character. So even your code would not have replaced all the characters, only the first one of each. If you want to replace all occurrences, you have to use a regular expression with theglobal modifier.