The common JavaScript regular expressions are collected and sorted out, which is necessary for form validation processing.
1 regular user name
//User name regular, 4 to 16 digits (letters, numbers, underscores, minus signs) var uPattern = /^[a-zA-Z0-9_-]$/; //Output true console.log(uPattern.test("aim1"));
2 cipher strength regular
//Password strength is regular, at least 6 digits, including at least 1 uppercase letter, 1 lowercase letter, 1 number, 1 special character var pPattern = /^.*(?=.)(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/; //Output true console.log("=="+pPattern.test("aim1#"));
3 integer regular
//Positive integer regular var posPattern = /^\d+$/; //Negative integer regular var negPattern = /^-\d+$/; //Integer regular var intPattern = /^-?\d+$/; //Output true console.log(posPattern.test("42")); //Output true console.log(negPattern.test("-42")); //Output true console.log(intPattern.test("-42"));
4-digit regular
It can be an integer or a floating-point number
//Positive regularity var posPattern = /^\d*\.?\d+$/; //Negative regularity var negPattern = /^-\d*\.?\d+$/; //Digital regularization var numPattern = /^-?\d*\.?\d+$/; console.log(posPattern.test("42.2")); console.log(negPattern.test("-42.2")); console.log(numPattern.test("-42.2"));
5 regular email
//Email regularization var ePattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z])$/; //Output true console.log(ePattern.test("[email protected]"));
6 regular mobile number
//Mobile number regular var mPattern = /^(((13[0-9])|(15[0-9])|(18[0-9])|(17[0-9]))+\d)$/;//Output true console.log(mPattern.test("18300000000"));
7 regular ID number
//ID number (18 digits) regular var cP = /^[1-9]\d(18|19|([23]\d))\d((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d[0-9Xx]$/; //Output true console.log(cP.test("21072619960605371X"));
8 URL regularization
//URL regularization var urlP= /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.])([\/\w \.-]*)*\/?$/; //Output true console.log(urlP.test("http://www.aimacademy.cn"));
9 IPv4 address regular
//ipv4 address regular var ipP = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.)(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; //Output true console.log(ipP.test("124.243.204.202"));
10 hex color regular
//RGB Hex color regular var cPattern = /^#?([a-fA-F0-9]|[a-fA-F0-9])$/; //Output true console.log(cPattern.test("#ff6700"));
11 date regular
//Date regular, simple judgment, without month and date judgment var dP1 = /^\d(\-)\d\1\d$/; //Output true console.log(dP1.test("2017-07-16")); //Output true console.log(dP1.test("2017-15-16")); //Date regular, complex decision var dP2 = /^(?:(?!0000)[0-9]-(?:(?: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](?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/; //Output true console.log(dP2.test("2017-07-16")); //Output false console.log(dP2.test("2017-15-16")); //Output false console.log(dP2.test("2017-02-29"));
12 QQ number regular
//QQ number regular, 5 to 11 bits var qqPattern = /^[1-9][0-9]$/; //Output true console.log(qqPattern.test("157116203"));
13 micro signal regularization
//Micro signal regular, 6-20 bits, start with letter, letter, number, minus sign, underline var wxPattern = /^[a-zA-Z]([-_a-zA-Z0-9])+$/; //Output true console.log(wxPattern.test("guoliangjun003"));
14 regular license plate number
//Regular license plate number var cPattern = /^[Beijing, Tianjin, Hu, Yu, Ji, Yu, Yun, Liao, Hei, Xiang, Wan, Lu, Xin, Su, Zhe, Gan, GUI, Gan, Jin, Meng, Shan, Ji, min, GUI, Yue, Qing, Chuan, Ning, Qiong, envoys and envoys A-Z][A-Z][A-Z0-9][A-Z0-9 Hang University Police Hong Kong and Macao]$/; //Output true console.log(cPattern.test("Liao G39006"));
15 include Chinese regular
//Include Chinese regular var cnPattern = /[\u4E00-\u9FA5]/; //Output true console.log(cnPattern.test("AI code 123"));