Regular expression for removing whitespaces

前端 未结 6 1564
悲&欢浪女
悲&欢浪女 2020-12-29 09:12

I have some text which looks like this -

\"    tushar is a good      boy     \"

Using javascript I want to remove all the extra white spac

6条回答
  •  粉色の甜心
    2020-12-29 09:58

    Since everyone is complaining about .trim(), you can use the following:

    str.replace(/\s+/g,' ' ).replace(/^\s/,'').replace(/\s$/,'');

    JSFiddle

提交回复
热议问题