//为String对象原型添加trim方法,去掉字符串前后的空格String.prototype.trim = function(){ // 用正则表达式将前后空格,用空字符串替代。 return this.replace(/(^\s*)|(\s*$)/g, "");}
//------------------------------------------------------------------------------------------------------
例子:<script>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 + ")");
</script>
Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved. 闵涛 E_mail:admin@mintao.net(欢迎提供学习资源)
鄂公网安备 42011102001154号
站长:MinTao ICP备案号:鄂ICP备11006601号-18