javascript去空格

来源:互联网 发布:数码兽传说 网络侦探 编辑:程序博客网 时间:2024/04/30 12:38
// 增加一个名为 trim 的函数作为// String 构造函数的原型对象的一个方法。String.prototype.trim = function(){    // 用正则表达式将前后空格    // 用空字符串替代。    return this.replace(/(^/s*)|(/s*$)/g, "");}// 有空格的字符串var s = "    leading and trailing spaces    ";// 显示 "    leading and trailing spaces     (35)"window.alert(s + " (" + s.length + ")");// 删除前后空格s = s.trim();// 显示"leading and trailing spaces (27)"window.alert(s + " (" + s.length + ")");
 
原创粉丝点击