I have a text that looks like this:
" tushar is a good boy "
Using javascript, I want to remove all the extra spaces in the string.
As a result, the line should not have many spaces, but only one. In addition, the beginning and the end must not have any spaces. So my final result should look like this:
"tushar is a good boy"
I am currently using the following code:
str.replace(/(\s\s\s*)/g, ' ')
This obviously fails because it does not care about white spaces at the beginning and end of the line.
source share