15个实用实用正则(小哥进来看看?)
// 1 用户名正则
//用户名正则,4到16位(字母,数字,下划线,减号)
var uPattern = /1{4,16}KaTeX parse error: Undefined control sequence: \d at position 146: …*(?=.{6,})(?=.*\̲d̲)(?=.*[A-Z])(?=…%^&? ]).KaTeX parse error: Expected 'EOF', got '#' at position 46: …est("caibaojian#̲"));//输出 true /…/;
//负整数正则
var negPattern = /^-\d+KaTeX parse error: Undefined control sequence: \d at position 32: …tPattern = /^-?\̲d̲+/;
console.log(posPattern.test(“42”));//输出 true
console.log(negPattern.test("-42"));//输出 true
console.log(intPattern.test("-42"));//输出 true
// 4 数字正则
//正数正则
var posPattern = /^\d*.?\d+KaTeX parse error: Undefined control sequence: \d at position 31: …egPattern = /^-\̲d̲*\.?\d+/;
//数字正则
var numPattern = /^-?\d*.?\d+KaTeX parse error: Undefined control sequence: \- at position 172: … /^([A-Za-z0-9_\̲-̲\.])+\@([A-Za-z…/;
console.log(ePattern.test(“815682884@qq.com”));//输出 true
// 6 手机号码正则
//手机号正则
var mPattern = /^1[34578]\d{9}KaTeX parse error: Undefined control sequence: \d at position 98: …ar cP = /^[1-9]\̲d̲{5}(18|19|([23]…/;
console.log(cP.test(“11010519880605371X”));//输出 true
// 8 URL正则
//URL正则
var urlP= /^((https?|ftp|file)😕/)?([\da-z.-]+).([a-z.]{2,6})([/\w .-])/?KaTeX parse error: Can't use function '\.' in math mode at position 138: …1]?[0-9][0-9]?)\̲.̲){3}(?:25[0-5]|…/;
//输出 true
console.log(ipP.test(“115.28.47.26”));
// 十六进制颜色正则
//RGB Hex颜色正则
var cPattern = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})KaTeX parse error: Expected 'EOF', got '#' at position 31: …cPattern.test("#̲f8f8f8"));//输出 …/;
console.log(dP1.test(“2017-05-11”));//输出 true
console.log(dP1.test(“2017-15-11”));//输出 true
//日期正则,复杂判定
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) / ; c o n s o l e . l o g ( d P 2. t e s t ( " 2017 − 02 − 11 " ) ) ; / / 输 出 t r u e c o n s o l e . l o g ( d P 2. t e s t ( " 2017 − 15 − 11 " ) ) ; / / 输 出 f a l s e c o n s o l e . l o g ( d P 2. t e s t ( " 2017 − 02 − 29 " ) ) ; / / 输 出 f a l s e / / 12 Q Q 号 码 正 则 / / Q Q 号 正 则 , 5 至 11 位 v a r q q P a t t e r n = / [ 1 − 9 ] [ 0 − 9 ] 4 , 10 /; console.log(dP2.test("2017-02-11"));//输出 true console.log(dP2.test("2017-15-11"));//输出 false console.log(dP2.test("2017-02-29"));//输出 false // 12 QQ号码正则 //QQ号正则,5至11位 var qqPattern = /^[1-9][0-9]{4,10} /;console.log(dP2.test("2017−02−11"));//输出trueconsole.log(dP2.test("2017−15−11"));//输出falseconsole.log(dP2.test("2017−02−29"));//输出false//12QQ号码正则//QQ号正则,5至11位varqqPattern=/[1−9][0−9]