Adding Trim functions to Javascript String Prototype

Some times, a little RegExp is a beautiful thing…

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

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

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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.