`
thunderbow
  • 浏览: 154107 次
  • 性别: Icon_minigender_1
  • 来自: beijing
社区版块
存档分类
最新评论

为javascript添加trim函数

阅读更多
强类型语言的字符串类都有去除空格的trim函数,而js默认是没有的,在你加载的js中添加下面的函数后,脚本中就直接可以使用trim(),ltrim()和rtrim()函数了,这些函数使用正则表达式实现效率还好!符合w3c标准!
<script type='text/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>
 
分享到:
评论
1 楼 xwq18 2010-06-02  
  

相关推荐

Global site tag (gtag.js) - Google Analytics