May 13, 2010

triming leading and trailing whitespace in javascript

var result = text.replace( /^(\s|\u00A0)+|(\s|\u00A0)+$/g, "" )

String.prototype.trim = function () {
    return this.replace(
        /^\s*(\S*(\s+\S+)*)\s*$/, "$1"); 
};