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

Posted: March 20, 2023

Return to the snippets listing