js中去除字符串两端的空格可以用正则表达式来实现:
String.prototype.trim=function() { return this.replace(/(^\s*)|(\s*$)/g, ""); }当然用法很简单,
var str =" Hi,My Name is Water! "; str.trim();
当然也可以自己写一个函数用,或者有些人会习惯这样:
function trim(stri) { return stri.replace(/(^\s*)|(\s*$)/g, ""); }
var str =" Hi,My Name is Water! ";
trim(str);【相关推荐】
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号