trim() method for JavaScript
Posted: Tue Aug 30, 2005 5:19 pm
Not sure this even qualifies as a snippet lol....
If anyone knows how to make it return it's value back to the object that called it please say.... i.e. How to make "someString" automatically change by calling the method. I only recently learned how to create methods for built-in objects.
Code: Select all
String.prototype.trim = function()
{
return this.replace(/^\s+/, '').replace(/\s+$/, '');
}
//Example usage
var someString = ' foo '; //Whitespace galore
var newString = someString.trim();
alert(newString);