Skip to content
Advertisement

replace multiple spaces in a string with a single space

I have a file with has several spaces among the words at some point. I need to clean the file and replace existing multi-spaced sequences with one space only. I have written the following statement which does not work at all, and it seems I’m making a big mistake.

JavaScript

My file is very simple. Here is a part of it:

JavaScript

Advertisement

Answer

Your regex replaces any number of spaces (including zero) with a space. You should only replace two or more (after all, replacing a single space with itself is pointless):

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement