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

100 thoughts on “Trim and reuse RegExp matches to update string”

  1. i need a payday loan direct lender, need a loan but have no credit. i need home loan with bad credit i need loan i need a loan now i need loan 2018, cash advances online toronto, cash advance, cash advance, payday advance loans without bank account. Money management describes financial affairs, accepts deposits.

    Reply

Leave a Comment