El primer tipo: reemplazo de verificación de bucle
// para que el usuario llame a la (s) TRIM (S) {return Trimright (Trimleft (s)); } // Eliminar el blanco en la función izquierda Trimleft (s) {if (s == null) {return ""; } var whitespace = new String ("/t/n/r"); var str = nueva cadena (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); } return str; } // Eliminar el blanco en la función www.vevb.com Trimright (s) {if (s == null) return ""; var whitespace = new String ("/t/n/r"); var str = nueva cadena (s); if (whitespace.indexof (str.charat (str.length -1))! = -1) {var i = str.length -1; while (i> = 0 && whitespace.indexof (str.charat (i))! = -1) {i--; } str = str.substring (0, i+1); } return str; }El segundo tipo: reemplazo 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> // Retire el espacio izquierdo; función ltrim (s) {return s.replace (/(^/s*)/g, "");} // elimina el espacio correcto; función rtrim (s) {return s.replace (/(/s*$)/g, "");} // elimina los espacios izquierdo y derecho; Trim (s) de función {return s.replace (/(^/s*) | (/s*$)/g, "");}El tercer tipo: usa jQuery
$ .trim (str)
La implementación interna de jQuery es:
función trim (str) {return str.replace (/^(/s |/u00a0)+/, ''). reemplazar (/(/s |/u00a0)+$/, ''); }Cuarto: usa Motools
función trim (str) {return str.replace (/^(/s |/xa0)+| (/s |/xa0)+$/g, ''); }Quinto: cuerdas de recorte
función 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); romper; }} return str; } // ----------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------- (vret.lastIndexof ('') == parseInt (vret.length) - 1)) {vret = vret.substring (0, parseInt (vret.length) - 1); } else {return vret; }}}Lo anterior es todo el contenido de este artículo. Para obtener más información sobre JavaScript, puede consultar: "Tutorial de referencia de JavaScript" y "Guía de estilo de código JavaScript". También espero que todos apoyen más a Wulin.com.