<div class="cnblogs_code">
<div class="cnblogs_code">
1用户名正则 用户名正则,4到16位(字母,数字,下划线,减号) uPattern = /^[a-zA-Z0-9_-]{4,16}$/输出 true console.log(uPattern.test("iFat3"2包括至少1个大写字母,1个小写字母,1个数字,1个特殊字符 pPattern = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/输出 true console.log("=="+pPattern.test("iFat3#"3整数正则 整数正则 posPattern = /^\d+$/整数正则 negPattern = /^-\d+$/整数正则 intPattern = /^-?\d+$/输出 true console.log(posPattern.test("42"输出 true console.log(negPattern.test("-42"输出 true console.log(intPattern.test("-42"4<span style="color: #008000">//<span style="color: #008000">正数正则
<span style="color: #0000ff">var posPattern = /^\d.?\d+$/<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">负数正则
<span style="color: #0000ff">var negPattern = /^-\d.?\d+$/<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">数字正则
<span style="color: #0000ff">var numPattern = /^-?\d.?\d+$/<span style="color: #000000">;
console.log(posPattern.test("42.2"<span style="color: #000000">));
console.log(negPattern.test("-42.2"<span style="color: #000000">));
console.log(numPattern.test("-42.2"<span style="color: #000000">));
5<span style="color: #000000"> Email正则
<span style="color: #008000">//<span style="color: #008000">Email正则
<span style="color: #0000ff">var ePattern = /^([A-Za-z0-9-.])+\@([A-Za-z0-9-.])+.([A-Za-z]{2,4})$/<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">输出 true
console.log(ePattern.test("65974040@qq.com"<span style="color: #000000">));
6<span style="color: #000000"> 手机号码正则
<span style="color: #008000">//<span style="color: #008000">手机号正则
<span style="color: #0000ff">var mPattern = /^[1][3][0-9]{9}$/<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">输出 true
console.log(mPattern.test("13900000000"<span style="color: #000000">));
7<span style="color: #000000"> 身份证号正则
<span style="color: #008000">//<span style="color: #008000">身份证号(18位)正则
<span style="color: #0000ff">var cP = /^[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]$/<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">输出 true
console.log(cP.test("11010519880605371X"<span style="color: #000000">));
8<span style="color: #000000"> URL正则
<span style="color: #008000">//<span style="color: #008000">URL正则
<span style="color: #0000ff">var urlP= /^((https?|ftp|file):\/\/)?([\da-z.-]+).([a-z.]{2,6})([\/\w .-])*\/?$/<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">输出 true
console.log(urlP.test("http://42du.cn"<span style="color: #000000">));
9<span style="color: #000000"> IPv4地址正则
<span style="color: #008000">//<span style="color: #008000">ipv4地址正则
<span style="color: #0000ff">var ipP = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">输出 true
console.log(ipP.test("115.28.47.26"<span style="color: #000000">));
10<span style="color: #000000"> 十六进制颜色正则
<span style="color: #008000">//<span style="color: #008000">RGB Hex颜色正则
<span style="color: #0000ff">var cPattern = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">输出 true
console.log(cPattern.test("#b8b8b8"<span style="color: #000000">));
11<span style="color: #000000"> 日期正则
<span style="color: #008000">//<span style="color: #008000">日期正则,简单判定,未做月份及日期的判定
<span style="color: #0000ff">var dP1 = /^\d{4}(-)\d{1,2}\1\d{1,2}$/<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">输出 true
console.log(dP1.test("2017-05-11"<span style="color: #000000">));
<span style="color: #008000">//<span style="color: #008000">输出 true
console.log(dP1.test("2017-15-11"<span style="color: #000000">));
<span style="color: #008000">//<span style="color: #008000">日期正则,复杂判定
<span style="color: #0000ff">var dP2 = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">输出 true
console.log(dP2.test("2017-02-11"<span style="color: #000000">));
<span style="color: #008000">//<span style="color: #008000">输出 false
console.log(dP2.test("2017-15-11"<span style="color: #000000">));
<span style="color: #008000">//<span style="color: #008000">输出 false
console.log(dP2.test("2017-02-29"<span style="color: #000000">));
12<span style="color: #000000"> QQ号码正则
<span style="color: #008000">//<span style="color: #008000">QQ号正则,5至11位
<span style="color: #0000ff">var qqPattern = /^[1-9][0-9]{4,10}$/<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">输出 true
console.log(qqPattern.test("65974040"<span style="color: #000000">));
13<span style="color: #000000"> 微信号正则
<span style="color: #008000">//<span style="color: #008000">微信号正则,6至20位,以字母开头,字母,数字,减号,下划线
<span style="color: #0000ff">var wxPattern = /^a-zA-Z+$/<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">输出 true
console.log(wxPattern.test("RuilongMao"<span style="color: #000000">));
14<span style="color: #000000"> 车牌号正则
<span style="color: #008000">//<span style="color: #008000">车牌号正则
<span style="color: #0000ff">var cPattern = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">输出 true
console.log(cPattern.test("京K39006"<span style="color: #000000">));
15<span style="color: #000000"> 包含中文正则
<span style="color: #008000">//<span style="color: #008000">包含中文正则
<span style="color: #0000ff">var cnPattern = /[\u4E00-\u9FA5]/<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">输出 true
console.log(cnPattern.test("42度"));
<pre class="best-text mb-10">