Regular expressions [xkcd] (I feel like him ;)):
s = s.replace(/[\[\]&]+/g, '');
Reference:
- MDN –
string.replace
- MDN – Regular Expressions
- http://www.regular-expressions.info/
Side note:
JavaScript’s replace
function 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 the g
lobal modifier.