Le premier type: Remplacement de vérification de boucle
// pour que l'utilisateur appelle la fonction de fonction (s) {return trimRight (trimbleft (s)); } // Retirez le blanc sur la fonction gauche trimbleft (s) {if (s == null) {return ""; } var whitespace = new String ("/ t / n / r"); var str = nouvelle chaîne (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; } // Supprimez le blanc à droite www.vevb.com Fonction TrimRight (S) {if (s == null) return ""; var whitespace = new String ("/ t / n / r"); var str = nouvelle chaîne (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; }Le deuxième type: remplacement régulier
<Script linguisse = "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> // Retirez l'espace gauche; fonction ltrim (s) {return s.replace (/ (^ / s *) / g, "");} // supprimer le bon espace; fonction rtrim (s) {return s.replace (/ (s * $) / g, "");} // supprimer les espaces gauche et droit; Fonction Trim (s) {return s.replace (/ (^ / s *) | (/ s * $) / g, "");}Le troisième type: utilisez jQuery
$ .trim (str)
L'implémentation interne de jQuery est:
Fonction Trim (str) {return str.replace (/ ^ (/ s | / u00a0) + /, ''). Remplace (/ (/ s | / u00a0) + $ /, ''); }Quatrième: Utilisez Motools
Fonction Trim (str) {return str.replace (/ ^ (/ s | / xa0) + | (/ s | / xa0) + $ / g, ''); }Cinquième: Croping Strings
Fonction Trim (str) {str = str.replace (/ ^ (/ s | / u00a0) + /, ''); pour (var i = str.length-1; i> = 0; i -) {if (// s / .test (str.charat (i))) {str = str.substring (0, i + 1); casser; }} return str; } // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 3 } else {return vret; }}}Ce qui précède est l'intégralité du contenu de cet article. Pour plus d'informations sur JavaScript, vous pouvez consulter: "JavaScript Reference Tutorial" et "JavaScript Code Style Guide". J'espère également que tout le monde soutiendra davantage Wulin.com.