Javascript String Remove white space

Description

Javascript String Remove white space


String.prototype.clean = function() {
  return this.replace(/\s+/g, " ").replace(/^([\s]+)|([\s]+)$/gm, "");
}

let originalString = "  The quick      brown fox jumped      over the small frog.  ";
let trimmedString = originalString.clean();

console.log( "Original String length: " + originalString.length);
console.log( "Final String length: " + trimmedString.length);
console.log( "Final String: " + trimmedString);



PreviousNext

Related