Trim whitespace in javascript
Tuesday, August 08 2006 - 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, ''); }
}
// 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