0

This source code covers tabs, newlines, etc,
Just replace \s\s+ with ‘ ‘:

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

Code with response time,

str.replace( / +/g, ' ' ) -> 380ms
str.replace( /\s\s+/g, ' ' ) -> 390ms
str.replace( / {2,}/g, ' ' ) -> 470ms
str.replace( / +/g, ' ' ) -> 790ms
str.replace( / +(?= )/g, ' ') -> 3250ms
Bikash Changed status to publish November 28, 2021
Add a Comment