日期:2014-05-16  浏览次数:20375 次

js 正则表达式 邮箱、字符串、URL格式匹配
邮箱:
var regm = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
           //验证Mail的正则表达式,^[a-zA-Z0-9_-]:开头必须为字母,下划线,数字,
           if (!mail.match(regm))
                {
                  alert("邮箱地址格式错误或含有非法字符!\n请检查!");
                  document.publish.mail.select();    
                  return false;
                 }
无符号字符串:
           /^[^\s]{1}[^-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*$/
URL:
           /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/
匹配中文字符的正则表达式: [\u4e00-\u9fa5]
匹配双字节字符(包括汉字在内):[^\x00-\xff]
应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
          
           String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}

表1.常用的元字符                                                            代码                        说明                                                                .                        匹配除换行符以外的任意字符                                                                \w                        匹配字母或数字或下划线或汉字                                                                \s                        匹配任意的空白符                                                    &nbs