从逗号和任何字母之间的字符串中删除空格

Remove white spaces from string between comma and any letter

我很少使用 RegExp 和 "string".match,所以我不太确定如何将它们用于一些复杂的事情 things.Here 是我想做和不想做的事情不知道该怎么做。 这里我有一个字符串 javascript.

var str= " I would like to know how to use RegExp    ,    string.match    and  string.replace"

我想删除逗号和任何 letter.So 之间的所有空格,此字符串将如下所示。

    str= " I would like to know how to use RegExp,string.match    and  string.replace"

我只知道如何使用这个删除字符串中的所有空格-->

str = str.replace(/\s/g, "")

应该可行:

str = str.replace(/\s*,\s*/g, ",");

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, ',')
);

您可以尝试使用正则表达式并在 https://regex101.com

上获得有关语言功能的体面文档

这是 this.lau_ 对您的问题的解决方案:https://regex101.com/r/aT7pS5/1

只需使用正则表达式:

\s*,\s*

DEMO