Remove white spaces from string between comma and any letter

前端 未结 4 456
半阙折子戏
半阙折子戏 2021-01-06 23:47

I use RegExp and \"string\".match very rarely so I\'m not so sure how to use them for some little bit complex things.Here\'s what I would like to do and don\'t know how to d

相关标签:
4条回答
  • 2021-01-06 23:58

    Simply use RegEx:

    \s*,\s*
    

    DEMO

    0 讨论(0)
  • 2021-01-06 23:59

    That should work:

    str = str.replace(/\s*,\s*/g, ",");
    
    0 讨论(0)
  • 2021-01-07 00:02

    You can play around with regex expressions and get a decent documentation of the language features on https://regex101.com

    Here is this.lau_'s solution of your problem: https://regex101.com/r/aT7pS5/1

    0 讨论(0)
  • 2021-01-07 00:06

    var str = " I would like to know how to use RegExp    ,    string.match    and  string.replace";
    
    console.log(
      str
    );
    console.log(
      str
      //Replace double space with single
      .replace(/  +/ig, ' ')
    );
    console.log(
      str
      //Replace double space with single
      .replace(/  +/ig, ' ')
      //Replace any amount of whitespace before or after a `,` to nothing
      .replace(/\s*,\s*/ig, ',')
    );

    0 讨论(0)
提交回复
热议问题