15 common js regular expressions, such as usernames and passwords
Collected and sorted out 15 common javaScript regular expressions, including user name, password strength, integer, number, Email address (Email), mobile phone number, ID card number, URL address, IPv4 address, hexadecimal color, date, QQ number, number, license plate number, and Chinese regular expression. Form verification is required.
1 username regular
// Username regular, 4 to 16 characters (letters, numbers, underlines, minus signs) var uPattern =/^ [a-zA-Z0-9 _-] {4, 16} $/; // output trueconsole. log (uPattern. test ("iFat3 "));
2. Regular password strength
// The password strength is regular, with at least 6 characters, including at least 1 uppercase letter, 1 lowercase letter, 1 digit, and 1 Special Character var pPattern =/^ .*(? =. {6 ,})(? =. * \ D )(? =. * [A-Z]) (? =. * [A-z]) (? = .*[! @ # $ % ^ &*? ]). * $/; // Output trueconsole. log ("=" + pPattern. test ("iFat3 #"));
3 integer regular
// Positive integer regular var posPattern =/^ \ d + $/; // negative integer regular var negPattern =/^-\ d + $ /; // integer regular var intPattern =/^ -? \ D + $/; // output trueconsole. log (posPattern. test ("42"); // output trueconsole. log (negPattern. test ("-42"); // output trueconsole. log (intPattern. test ("-42 "));
4. digit Regular Expression
It can be an integer or a floating point number.
// Positive number regular var posPattern =/^ \ d *\.? \ D + $/; // negative number regular var negPattern =/^-\ d *\.? \ D + $/; // numeric regular var numPattern =/^ -? \ D *\.? \ D + $/; console. log (posPattern. test ("42.2"); console. log (negPattern. test ("-42.2"); console. log (numPattern. test ("-42.2 "));
5. Email RegEx
// Email regular var ePattern =/^ ([A-Za-z0-9 _ \-\.]) + \ @ ([A-Za-z0-9 _ \-\.]) + \. ([A-Za-z] {2, 4}) $ // output trueconsole. log (ePattern. test (65974040@qq.com ));
6. Mobile phone number Regular Expression
// Mobile phone number regular var mPattern =/^ (13 [0-9]) | (14 [5 | 7]) | (15 ([0-3] | [5-9]) | (18 [-9]) \ d {8} $/; // output trueconsole. log (mPattern. (test ("18600000000 "));
7. ID card number regular
// ID card number (18 bits) Regular 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] $/; // output trueconsole. log (cP. test ("11010519880605371X "));
8 URL regular
// ID card number (18 bits) Regular 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] $/; // output trueconsole. log (cP. test ("11010519880605371X "));
9 IPv4 address regular
// Ipv4 address regular 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]?) $ // Output trueconsole. log (ipP. test ("115.28.47.26 "));
10 hexadecimal color regular
// RGB Hex color regular var cPattern =/^ #? ([A-fA-F0-9] {6} | [a-fA-F0-9] {3}) $/; // output trueconsole. log (cPattern. test ("# b8b8b8 "));
11. Date Regular Expression
// Date regular expression, simple determination, no determination of the month and date var dP1 =/^ \ d {4 }(\-) \ d {1, 2} \ 1 \ d {1, 2} $/; // output trueconsole. log (dP1.test (""); // output trueconsole. log (dP1.test ("2017-15-11"); // Date regular, complex determination 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 [1, 13578] | 1 [02])-31) | (? : [0-9] {2 }(? : 0 [48] | [2468] [048] | [13579] [26]) | (? : 0 [48] | [2468] [048] | [13579] [26]) 00)-02-29) $ // output trueconsole. log (dP2.test (""); // outputs falseconsole. log (dP2.test ("2017-15-11"); // outputs falseconsole. log (dP2.test ("2017-02-29 "));
12 QQ number regular
// QQ number regular expression, 5 to 11 bits var qqPattern =/^ [1-9] [0-9] {} $/; // output trueconsole. log (qqPattern. (test ("65974040 "));
Regular Expression No. 13
// Regular number, 6 to 20 digits, starting with a letter, letter, number, minus sign, underline var wxPattern =/^ [a-zA-Z] ([-_ a-zA-Z0-9] {5, 19}) + $ // output trueconsole. log (wxPattern. test ("RuilongMao "));
14 license plate number regular
// License plate number regular var cPattern =/^ [Beijing, Shanghai, Chongqing, Hebei, Yu cloud, Liao, black, Hunan, Anhui, new Jiangsu, Zhejiang, Jiangxi, Hubei, Guangxi, Gansu, Mongolia, Shaanxi, Jilin, Guizhou, Guangdong, Qinghai, Sichuan, ningqiong, lead A-Z] {1 }[ a-Z] {1} [A-Z0-9] {4} [A-Z0-9 school police Hong Kong and Macao] {1} $ /; // output the trueconsole. log (cPattern. test (" K39006 "));
15 contains Chinese Regular Expressions
// Contains the Chinese regular var cnPattern =/[\ u4E00-\ u9FA5] // outputs trueconsole. log (cnPattern. test ("42 degrees "));
The above is a small Editor for you to collect and sort out 15 common javaScript regular expressions, I hope to help you, if you have any questions, please leave a message, the small editor will reply to you in time!