O primeiro tipo: substituição de verificação de loop
// para o usuário chamar a (s) acabamento (s) da função (s) {return Trimright (trimleft (s)); } // Remova o espaço em branco na função esquerda Trimleft (s) {if (s == null) {return ""; } var whiteSpace = new String ("/t/n/r"); var str = nova (s) string (s); if (whiteSpace.IndexOf (str.Charat (0))! = -1) {var j = 0, i = str.Length; while (j <i && whitespace.indexof (str.charat (j))! = -1) {j ++; } str = str.substring (j, i); } retornar str; } // Remova o espaço em branco na função direita www.vevb.com Trimright (s) {if (s == null) return ""; var whiteSpace = new String ("/t/n/r"); var str = nova (s) string (s); if (whiteSpace.IndexOf (str.Charat (str.Length -1))! = -1) {var i = str.Length -1; while (i> = 0 && whitaSpace.indexof (str.charat (i))! = -1) {i--; } str = str.substring (0, i+1); } retornar str; }O segundo tipo: substituição regular
<Script Language = "Javascript"> <!- String.prototype.trim = function () {return this.replace (/(^/s*) | (/s*$)/g, ""); } String.prototype.ltrim = function () {return this.replace (/(^/s*)/g, ""); } String.prototype.rtrim = function () {return this.replace (/(/s*$)/g, ""); } //-> </script> // remova o espaço esquerdo; função lTRIM (s) {return s.Replace (/(^/s*)/g, "");} // remova o espaço certo; função rTrim (s) {return s.Replace (/(/s*$)/g, "");} // remova os espaços esquerdo e direito; Função (s) {return s.Replace (/(^/s*) | (/s*$)/g, "");}O terceiro tipo: use jQuery
$ .Trim (STR)
A implementação interna do jQuery é:
Função TRIM (STR) {return str.replace (/^(/s |/u00a0)+/, ''). substituir (/(/s |/u00a0)+$/, ''); }Quarto: use motools
Função TRIM (STR) {return str.replace (/^(/s |/xa0)+| (/s |/xa0)+$/g, ''); }Quinto: Crepping Strings
Função Trim (str) {str = str.Replace (/^(/s |/u00a0)+/, ''); for (var i = str.Length-1; i> = 0; i-) {if (// s/.test (str.charat (i))) {str = str.substring (0, i+1); quebrar; }} retornar str; } // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- (vret.lastIndexOf ('') == parseint (vret.length) - 1)) {vret = vret.substring (0, parseint (vret.length) - 1); } else {return vret; }}}O acima é o conteúdo inteiro deste artigo. Para obter mais informações sobre JavaScript, você pode conferir: "Javascript Reference Tutorial" e "JavaScript Code Style Guide". Eu também espero que todos apoiem mais wulin.com.