I have to write several lines of code to remove delimiters from a string. Is there a more efficient way to remove all delimiters? Thanks.
str = str.toLowerCase();
str = str.replace(/ /g,'');
str = str.replace(/\*/g, '');
str = str.replace(/_/g, '');
str = str.replace(/#/g, '');
//etc....
I have to write several lines of code to remove delimiters from a string. Is there a more efficient way to remove all delimiters? Thanks.
str = str.toLowerCase();
str = str.replace(/ /g,'');
str = str.replace(/\*/g, '');
str = str.replace(/_/g, '');
str = str.replace(/#/g, '');
//etc....
Share
Improve this question
edited Jun 28, 2017 at 18:30
martin jakubik
4,1485 gold badges31 silver badges42 bronze badges
asked Jun 28, 2017 at 18:19
DavidDavid
3233 gold badges8 silver badges21 bronze badges
1
- 1 You can't just remove delimiters this way, they are paired and possibly escaped as a literal. – user557597 Commented Jun 28, 2017 at 18:37
1 Answer
Reset to default 8Use character class:
str = str.toLowerCase().replace(/[ *_#]/g, '');
http://www.regular-expressions.info/charclass.html