eve eve - 7 months ago 17
Javascript Question

Replace multiple whitespaces with single whitespace in JavaScript string

I have strings with extra whitespaces, each time there's more than only one whitespace I'd like it be only one.

Anyone?
I tried searching google, but nothing worked for me.

Thanks

Answer

Something like this:

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