Regular expression for removing whitespaces

前端 未结 6 1562
悲&欢浪女
悲&欢浪女 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:12

    This works nicely:

    function normalizeWS(s) {
        s = s.match(/\S+/g);
        return s ? s.join(' ') : '';
    }
    
    • trims leading whitespace
    • trims trailing whitespace
    • normalizes tabs, newlines, and multiple spaces to a single regular space

提交回复
热议问题