Trim and reuse RegExp matches to update string

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...

1 thought on “Trim and reuse RegExp matches to update string”

Leave a Comment