triming leading and trailing whitespace in javascript May 13, 2010 • Fred Yang var result = text.replace( /^(\s|\u00A0)+|(\s|\u00A0)+$/g, "" ) String.prototype.trim = function () { return this.replace( /^\s*(\S*(\s+\S+)*)\s*$/, "$1"); }; Please enable JavaScript to view the comments powered by Disqus.