regex replace space at begin and end and consecutive site:stackoverflow.com code example
Example: replace multiple spaces with single space javascript
string = string.replace(/\s\s+/g, ' ');
string = string.replace(/\s\s+/g, ' ');