I am trying to replace multiple spaces with as many ' ' as there are spaces. However, I do not want to replace single spaces.
let string = 'The quick brown fox jumped over a lazy dog';
My desired output is
"The quick brown fox jumped over a lazy dog"
I am able to do this by string.replace(/(\S)( )(\S)/g, "$1~$3").replace(/ /g, ' ').replace(/~/g, ' ');. But this requires the string to be scanned through three replace statements. I am replacing single spaces with ~, then all spaces with nbsp; and then finally reverting ~ to single spaces.
Is there an easier way of doing this by just one scan?