摘要
收集整理了15個經常使用的javaScript正則表達式,其中包括用戶名、密碼強度、整數、數字、電子郵件地址(Email)、手機號碼、身份證號、URL地址、 IPv4地址、 十六進制顏色、 日期、 QQ號碼、 微信號、車牌號、中文正則。表單驗證處理必備,趕忙收藏吧!java
用戶名正則
var
uPattern = /^[a-zA-Z0-9_-]{4,16}$/;
console.log(uPattern.test(
"iFat3"
));
|
密碼強度正則
var
pPattern = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@
#$%^&*? ]).*$/;
console.log(
"=="
+pPattern.test(
"iFat3#"
));
|
整數正則
var
posPattern = /^\d+$/;
var
negPattern = /^-\d+$/;
var
intPattern = /^-?\d+$/;
console.log(posPattern.test(
"42"
));
console.log(negPattern.test(
"-42"
));
console.log(intPattern.test(
"-42"
));
|
數字正則
能夠是整數也能夠是浮點數
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"
));
|
Email正則
var
ePattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
console.log(ePattern.test(
"65974040@qq.com"
));
|
手機號碼正則
var
mPattern = /^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/;
console.log(mPattern.test(
"18600000000"
));
|
身份證號正則
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"
));
|
URL正則
var
urlP= /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
|
IPv4地址正則
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"
));
|
十六進制顏色正則
var
cPattern = /^
#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;
console.log(cPattern.test(
"#b8b8b8"
));
|
日期正則
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"
));
|
QQ號碼正則
var
qqPattern = /^[1-9][0-9]{4,10}$/;
console.log(qqPattern.test(
"65974040"
));
|
微信號正則
var
wxPattern = /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/;
console.log(wxPattern.test(
"RuilongMao"
));
|
車牌號正則
var
cPattern = /^[京津滬渝冀豫雲遼黑湘皖魯新蘇浙贛鄂桂甘晉蒙陝吉閩貴粵青藏川寧瓊使領A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9掛學警港澳]{1}$/;
console.log(cPattern.test(
"京K39006"
));
|
包含中文正則
var
cnPattern = /[\u4E00-\u9FA5]/;
console.log(cnPattern.test(
"42度"
));
|
原文地址:http://funteas.com/topic/592f828e572bfb9c4d965ab0正則表達式