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 sExample = new String (" testing trim ")
alert(sExample.trim())