Bia Securities

Trim whitespace in javascript

by Jim Geurts on Aug.08, 2006

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, ”); }
}


Comments are closed.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...