String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); } /** * returns string with all leading and trailing characters * eliminated. */ function trim(str){ var s = new String(str); //trailing spaces while (s.length>0 && isSpaceCharacter(""+s.charAt(s.length-1))){ s = s.substring(0,s.length-1); } //leading spaces while (s.length>0 && isSpaceCharacter(""+s.charAt(0))){ s = s.substring(1); } return s } var spaces = " \t\r\n"+String.fromCharCode(160); function isSpaceCharacter(ch){ return spaces.indexOf(ch) >-1; }