Javascript String Prototype trim, trim left, trim right

Description

Javascript String Prototype trim, trim left, trim right


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

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

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

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

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



PreviousNext

Related