常用正则表达式(regular expression)
需要转义的符号
$()*+.[]?^{},|
IP地址(IP address) ipv4: RegExp: ^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$ Jquery: (/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.test(value) && (RegExp.$1 <256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256)); ipv6: RegExp: ^(([\da-fA-F]{1,4}):){7}([\da-fA-F]{1,4})$ Jquery: /^(([\da-fA-F]{1,4}):){7}([\da-fA-F]{1,4})$/.test(value); 字母数字或下划线(letter, digit or underline) RegExp: ^[a-zA-Z0-9_]*$ Jquery: /^[a-zA-Z0-9_]*$/.test(value); 3-20位字母或数字开头,允许字母数字下划线(start with letter or digit, length 3-20, allow underline) RegExp: ^[a-zA-Z0-9][a-zA-Z0-9_]{2,19}$ Jquery: /^[a-zA-Z0-9][a-zA-Z0-9_]{2,19}$/.test(value); 真实姓名验证,姓名只能为2-30个汉字(name verification, length 2-30 Chinese character) RegExp: ^[\u4e00-\u9FA5]{2,30}$ Jquery: /^[\u4e00-\u9FA5]{2,30}$/.test(value); 字符验证(all sorts of letter or digit or underline) RegExp: ^[\u0391-\uFFE5\w]+$ Jquery: /^[\u0391-\uFFE5\w]+$/.test(value); 手机号码验证(mobile phone number) RegExp: ^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$ Jquery: (length == 11 && /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/.test(value)); QQ号码验证(QQ) RegExp: ^[1-9][0-9]{4,}$ Jquery: /^[1-9][0-9]{4,}$/.test(value); 身份证号(ID) RegExp: (not perfect) ^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$ Jquery: var area={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"} var idcard,Y,JYM; var S,M; var idcard_array = new Array(); idcard_array = idcard.split(""); //地区检验 if(area[parseInt(idcard.substr(0,2))]==null) return Errors[4]; //身份号码位数及格式检验 switch(idcard.length){ case 15: if ( (parseInt(idcard.substr(6,2))+1900) % 4 == 0 || ((parseInt(idcard.substr(6,2))+1900) % 100 == 0 && (parseInt(idcard.substr(6,2))+1900) % 4 == 0 )){ ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//测试出生日期的合法性 } else { ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//测试出生日期的合法性 } if(ereg.test(idcard)) return Errors[0]; else return Errors[2]; break; case 18: //18 位身份号码检测 //出生日期的合法性检查 //闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9])) //平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8])) if ( parseInt(idcard.substr(6,4)) % 4 == 0 || (parseInt(idcard.substr(6,4)) % 100 == 0 && parseInt(idcard.substr(6,4))%4 == 0 )){ ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//闰年出生日期的合法性正则表达式 } else { ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//平年出生日期的合法性正则表达式 } if(ereg.test(idcard)) {//测试出生日期的合法性 //计算校验位 S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7 + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9 + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10 + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5 + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8 + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4 + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2 + parseInt(idcard_array[7]) * 1 + parseInt(idcard_array[8]) * 6 + parseInt(idcard_array[9]) * 3 ; Y = S % 11; M = "F"; JYM = "10X98765432"; M = JYM.substr(Y,1);//判断校验位 if(M == idcard_array[17]) return Errors[0]; //检测ID的校验位 else return Errors[3]; } else return Errors[2]; break; default: return Errors[1]; break; ZIP范围(ZIP range) RegExp: ^90[2-5]\d\{2\}-\d{4}$ Jquery: /^90[2-5]\d\{2\}-\d{4}$/.test(value); ZIP code US RegExp: \d{5}-\d{4}$|^\d{5}$ Jquery: /\d{5}-\d{4}$|^\d{5}$/.test(value); VIN Jquery: if (v.length !== 17) { return false; } var i, n, d, f, cd, cdv; var LL = ["A","B","C","D","E","F","G","H","J","K","L","M","N","P","R","S","T","U","V","W","X","Y","Z"]; var VL = [1,2,3,4,5,6,7,8,1,2,3,4,5,7,9,2,3,4,5,6,7,8,9]; var FL = [8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2]; var rs = 0; for(i = 0; i < 17; i++){ f = FL[i]; d = v.slice(i,i+1); if (i === 8) { cdv = d; } if (!isNaN(d)) { d *= f; } else { for (n = 0; n < LL.length; n++) { if (d.toUpperCase() === LL[n]) { d = VL[n]; d *= f; if (isNaN(cdv) && n === 8) { cdv = LL[n]; } break; } } } rs += d; } cd = rs % 11; if (cd === 10) { cd = "X"; } if (cd === cdv) { return true; } return false;