用逗号表删除单个空格的正则表达式

时间:2015-03-21 16:21:26

标签: javascript regex

我有这个:

Anchor, Island, Super Over,  Power Play

我想要这个:

Anchor,Island,Super Over, Power Play

Power Play有两个spce

1 个答案:

答案 0 :(得分:1)

这将用逗号替换逗号和单个空格。

> "Anchor, Island, Super Over,  Power Play".replace(/,\s/g, ",")
'Anchor,Island,Super Over, Power Play'

OR

这将用空字符串替换逗号后的一个或多个空格。

> "Anchor, Island, Super Over,  Power Play".replace(/,\s+/g, ",")
'Anchor,Island,Super Over,Power Play'