Trim String

Description: Handy function for trimming spaces off the beginning and end of a string in JavaScript.
Tested Platform: All Modern Browsers
Language: JavaScript
// Prototype version
String.prototype.trim = function() {
	return this.replace(/^s+|s+$/g, "");
};


// Stand alone version
function(str) {
	return str.replace(/^s+|s+$/g, "");
};

Submitted: December 11, 2012

Return to the snippets listing