regex - Efficient way to remove delimiters from string in JavaScript - Stack Overflow

I have to write several lines of code to remove delimiters from a string. Is there a more efficient way

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
Add a ment  | 

1 Answer 1

Reset to default 8

Use character class:

str = str.toLowerCase().replace(/[ *_#]/g, '');

http://www.regular-expressions.info/charclass.html

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744550137a4580247.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信