手机号(mobile phone)中国(严谨), 根据工信部2019年最新公布的手机号段
const reg = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/;
const str = "19119255642";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
Email(邮箱)
const reg = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
const str = "90203918@qq.com";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
座机(Tel Phone)电话(国内) 如: 0341-86091234
const reg = /^(?:(?:\d{3}-)?\d{8}|^(?:\d{4}-)?\d{7,8})(?:-\d+)?$/;
const str = "0936-4211235";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
身份证号(2代,18位数字),最后一位是校验位,可能为数字或字符X
const reg = /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/;
const str = "12345619991205131x";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
中文姓名
const reg = /^(?:[\u4e00-\u9fa5·]{2,16})$/;
const str = "韩小韩";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
英文姓名
const reg = /(^[a-zA-Z][a-zA-Z\s]{0,20}[a-zA-Z]$)/;
const str = "James Han";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
网址(URL)
const reg =
/^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?/;
const str = "https://www.vvhan.com";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
必须带端口号的网址(或IP)
const reg = /^((ht|f)tps?:\/\/)?[\w-]+(\.[\w-]+)+:\d{1,5}\/?$/;
const str = "https://www.vvhan.com:80";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
银行卡号(10到30位, 覆盖对公/私账户)
const reg = /^[1-9]\d{9,29}$/;
const str = "6222026006705354000";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线组合)
const reg = /^[a-zA-Z]\w{4,15}$/;
const str = "han_666666";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
中文/汉字
const reg = /^(?:[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])+$/;
const str = "易航博客";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
密码强度校验,最少6位,包括至少1个大写字母,1个小写字母,1个数字,1个特殊字符
const reg = /^\S*(?=\S{6,})(?=\S*\d)(?=\S*[A-Z])(?=\S*[a-z])(?=\S*[!@#$%^&*? ])\S*$/;
const str = "han@666vvx,";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
微信号(wx),6至20位,以字母开头,字母,数字,减号,下划线
const reg = /^[a-zA-Z][-_a-zA-Z0-9]{5,19}$/;
const str = "kd_-666";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
邮政编码(中国)
const reg = /^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\d{4}$/;
const str = "734500";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
中文和数字
const reg = /^((?:[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])|(\d))+$/;
const str = "易航好6啊";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
匹配连续重复的字符
const reg = /(.)\1+/;
const str = "112233";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
统一社会信用代码
const reg = /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/;
const str = "91110108772551611J";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
子网掩码(不包含 0.0.0.0)
const reg = /^(254|252|248|240|224|192|128)\.0\.0\.0|255\.(254|252|248|240|224|192|128|0)\.0\.0|255\.255\.(254|252|248|240|224|192|128|0)\.0|255\.255\.255\.(255|254|252|248|240|224|192|128|0)$/;
const str = "255.255.255.0";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
车牌号(新能源)
const reg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z](?:((\d{5}[A-HJK])|([A-HJK][A-HJ-NP-Z0-9][0-9]{4}))|[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳])$/;
const str = "京AD92035";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
车牌号(非新能源)
const reg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]$/;
const str = "京A00599";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
车牌号(新能源+非新能源)
const reg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/;
const str = "京A12345D";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
护照(包含香港、澳门)
const reg = /(^[EeKkGgDdSsPpHh]\d{8}$)|(^(([Ee][a-fA-F])|([DdSsPp][Ee])|([Kk][Jj])|([Mm][Aa])|(1[45]))\d{7}$)/;
const str = "s28233515";
console.log(`校验 ${reg.test(str) ? "正确" : "错误"}`);
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容