Adding trim() function to JavaScript String.prototype
November 18th, 2006 by Ivan Uzunov
To extend the String.prototype with the trim() function I use this line of code:
String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,'’) }
This is sample code how to use the trim() function:
var sMyVar = new String (" testing trim ");JavaScript, trim
alert(sMyVar.trim());
//use it with the value of a text box
document.getElementById("txtMyTextBox").value.trim();



