js中的正则

1 正则总结

   http://www.blogjava.net/onejavaer/articles/79070.html

2 判断中文、英文、数字      http://hi.baidu.com/stodbx2002/blog/item/95f1e4d4a21e660ea18bb7f1.html

    中文:var reg=/^[\u4E00-\u9FA5]+$/; 3 string去掉空格(用到了原型、对象的思想,String.prototype这种方式在高级程序设计中不建议)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>去掉多余空格</title>
</head>
<script type="text/javascript">
   function trimstring(strchange)
   {
      this.str=strchange;
      
   }
   
     trimstring.prototype={
        constructor:trimstring,
        trim:function() {this.str=this.str.replace(/(^\s+)|(\s+$)/g,"");},
        lrim:function() {this.str=this.str.replace(/^\s+/g,"");},
        rrim:function() {this.str=this.str.replace(/\s+$/g,"");},
        allrim:function() {this.str=this.str.replace(/\s+/g,"");},
        getstr:function(){return this.str;}
    };
   
   function detrim() //去掉左右空格,其他类似,调用相应函数,略
   {
       var a =new trimstring(document.getElementById("trim").value);
       a.trim();
       document.getElementById("trim").value=a.getstr();
   }

</script>

<body>
   <input id="trim" value="    两边空格    " /> <input type="button" value="两边空格" onclick="detrim()" />
   <input id="ltrim" value="    左边空格" /> <input type="button" value="左边空格" />
   <input id="rtrim" value="右边空格    " /> <input type="button" value="右边空格" />
   <input id="alltrim" value="    所有  空格    " /> <input type="button" value="所有空格" />
</body>
</html>

相关推荐