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

javascript - Replace all occurrences of double comma with a single comma - Stack Overflow

programmeradmin4浏览0评论

I need hello,,,,,,,,,,,world to bee hello,world

I have this but it only replaces 2 mas

s = s.replace(/\,\,/g,',');

How can I replace more than one ma after another with a single ma?

I need hello,,,,,,,,,,,world to bee hello,world

I have this but it only replaces 2 mas

s = s.replace(/\,\,/g,',');

How can I replace more than one ma after another with a single ma?

Share Improve this question edited Dec 13, 2014 at 16:59 j08691 208k32 gold badges269 silver badges280 bronze badges asked Dec 13, 2014 at 16:58 124697124697 21.9k69 gold badges197 silver badges319 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 12
s = s.replace(/,+/g,',');

A + in a regular expression means "one or more of the previous thing, in a row." So ,+ means "one or more mas in a row."

Also can use {2,} to specify match more than 1

s = s.replace(/,{2,}/g, ',');
发布评论

评论列表(0)

  1. 暂无评论