remove unwanted commas in JavaScript

后端 未结 9 1607
青春惊慌失措
青春惊慌失措 2020-12-14 13:14

I want to remove all unnecessary commas from the start/end of the string.

eg; google, yahoo,, , should become google, yahoo.

If pos

9条回答
  •  抹茶落季
    2020-12-14 13:44

    My take:

    var cleanStr = str.replace(/^[\s,]+/,"")
                      .replace(/[\s,]+$/,"")
                      .replace(/\s*,+\s*(,+\s*)*/g,",")
    

    This one will work with opera, internet explorer, whatever

    Actually tested this last one, and it works!

提交回复
热议问题