I have a method to remove the extra spaces between words and trim start and end of strings:
// Remove extra spaces between words and trim start and end of strings
String.prototype.trimmer = function() {
return String(this).replace(/^\s+|\s+$|\s+(?=\s)/g, '');
};
const text = " This , is!! !not ??trimmed:: :: correctly . . . ";
console.log(text.trimmer());
Now the issue is how we can put each of these signs , ! ? : .
to be right after the previous word and without any space.
So the desired result for the given text would be:
This, is!!! not?? trimmed:::: correctly...
First look for all the signs
, ! ? : .
(including spaces). Then use those "matches" to replace all spaces and only add one at the end. Not sure if you could achieve the same result with a single Regex. Like so it works.You can also achieve that with a series of regex replaces:
Output: