Remove white spaces from string between comma and any letter

前端 未结 4 492
半阙折子戏
半阙折子戏 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-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, ',')
    );

提交回复
热议问题