How can I trim the leading and trailing comma in javascript?

前端 未结 5 1275
忘掉有多难
忘掉有多难 2020-12-13 01:58

I have a string that is like below.

,liger, unicorn, snipe,

how can I trim the leading and trailing comma in javascript?

5条回答
  •  粉色の甜心
    2020-12-13 02:44

    In ECMAScript 5 and above, you can now do a one-liner

    ',liger, unicorn, snipe,'.split(',').map(e => e.trim()).filter(e => e).join(', ')
    

提交回复
热议问题