常用正则表达式(转载)
转载
1 用户名正则
?
1
2
3
4
|
var uPattern = /^[a-zA-Z0-9_-]{4,16}$/;
console.log(uPattern.test( "iFat3" ));
|
2 密码强度正则
?
1
2
3
4
|
var pPattern = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@ #$%^&*? ]).*$/;
console.log( "==" +pPattern.test( "iFat3#" ));
|
3 整数正则
?
1
2
3
4
5
6
7
8
9
10
11
12
|
var posPattern = /^\d+$/;
var negPattern = /^-\d+$/;
var intPattern = /^-?\d+$/;
console.log(posPattern.test( "42" ));
console.log(negPattern.test( "-42" ));
console.log(intPattern.test( "-42" ));
|
4 数字正则
可以是整数也可以是浮点数
?
1
2
3
4
5
6
7
8
9
|
var posPattern = /^\d*\.?\d+$/;
var negPattern = /^-\d*\.?\d+$/;
var numPattern = /^-?\d*\.?\d+$/;
console.log(posPattern.test( "42.2" ));
console.log(negPattern.test( "-42.2" ));
console.log(numPattern.test( "-42.2" ));
|
5 Email正则
?
1
2
3
4
|
var ePattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
console.log(ePattern.test(<a href= "mailto:65974040@qq.com" >65974040@qq.com</a>));
|
6 手机号码正则
?
1
2
3
4
|
var mPattern = /^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/;
console.log(mPattern.test( "18600000000" ));
|
7 身份证号正则
?
1
2
3
4
|
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]$/;
console.log(cP.test( "11010519880605371X" ));
|
8 URL正则
?
1
2
3
4
|
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]$/;
console.log(cP.test( "11010519880605371X" ));
|
9 IPv4地址正则
?
1
2
3
4
|
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]?)$/;
console.log(ipP.test( "115.28.47.26" ));
|
10 十六进制颜色正则
?
1
2
3
4
|
var cPattern = /^ #?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;
console.log(cPattern.test( "#b8b8b8" ));
|
11 日期正则
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
var dP1 = /^\d{4}(\-)\d{1,2}\1\d{1,2}$/;
console.log(dP1.test( "2017-05-11" ));
console.log(dP1.test( "2017-15-11" ));
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)$/;
console.log(dP2.test( "2017-02-11" ));
console.log(dP2.test( "2017-15-11" ));
console.log(dP2.test( "2017-02-29" ));
|
12 QQ号码正则
?
1
2
3
4
|
var qqPattern = /^[1-9][0-9]{4,10}$/;
console.log(qqPattern.test( "65974040" ));
|
13 微信号正则
?
1
2
3
4
|
var wxPattern = /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/;
console.log(wxPattern.test( "RuilongMao" ));
|
14 车牌号正则
?
1
2
3
4
|
var cPattern = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
console.log(cPattern.test( "京K39006" ));
|
15 包含中文正则
?
1
2
3
4
|
var cnPattern = /[\u4E00-\u9FA5]/;
console.log(cnPattern.test( "42度" ));
|
树立目标,保持活力,gogogo!
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。