Regular expression for removing whitespaces

前端 未结 6 1587
悲&欢浪女
悲&欢浪女 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 10:09

    try

    var str = "    tushar is a good      boy     ";
    str = str.replace(/^\s+|\s+$/g,'').replace(/(\s\s\s*)/g, ' ');
    

    first replace is delete leading and trailing spaces of a string.

提交回复
热议问题