最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论