String.prototype.LTrim = function() { return this.replace(/^\s*(.*)/gmi, "$1"); } String.prototype.RTrim = function() { return this.replace(/(.*?)\s*$/gmi, "$1"); } String.prototype.Trim = function() { return this.replace(/^\s*(.*?)\s*$/gmi, "$1"); } // Se prefir, a função Trim também pode ser assim: String.prototype.Trim = function() { return this.TLrim().RTrim(); } Para utilizar faça: var myStr = " Meu teste da função TRIM "; strLen = "\nTamanho original: " + myStr.length; strLen += "\nCom LTrim: " + myStr.LTrim().length; strLen += "\nCom RTrim: " + myStr.RTrim().length; strLen += "\nCom Trim: " + myStr.Trim().length; alert("String: " + myStr + strLen);