Trim whitespace in javascript

Occasionally I need these functions, and I'm surprised that I haven't posted it yet.  anyway, enjoy...

// Add left trim, right trim, and trim functions
if (!String.prototype.lTrim) {
    String.prototype.lTrim = function() { return this.replace(/^\s*/, ''); }
}
if (!String.prototype.rTrim) {
    String.prototype.rTrim = function() { return this.replace(/\s*$/, ''); }
}
if (!String.prototype.trim) {
    String.prototype.trim = function() { return this.lTrim().rTrim(); }
}

// This will remove all whitespace from a string
if (!String.prototype.removeAllWhitespace) {
    String.prototype.removeAllWhitespace = function() { return this.replace(/\s+/g, ''); }
}

Kick it! Del.icio.us  Digg It!  StumbleUpon  Reddit  NewsVine  Furl  BlinkList  Ma.gnolia  Technorati

Comments