javascript 中的trim实现
2004-11-16 17:15
363 查看
<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 + ")");
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 + ")");
相关文章推荐
- 图片或文字或图文混合连续滚动
- 测试能用的blog界面修改方法
- 测试能用的blog界面修改方法
- checkbox全选
- 通过JScript 来读取服务端文件的方法
- 网页对联广告
- Window下JSP环境的搭建
- JS错误代码解释大全
- JSP生成的彩色验证码
- Page caching discussion
- 蛙蛙推荐:WEB编码规范
- javascript小技巧
- 免费国际域名动态IP捆绑
- IE拖放漏洞详解一
- JavaScript[对象.属性]集锦
- Javascript浅拷贝与深拷贝实现
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- JavaScript的Prototype实现
- JavaScript 的几个 tip
- 介绍一个Open Source的Javascript UI Lib