Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the common js regular expressions?

Shulou Source: shulou.com Published: 2022-06-02 04:07:16 10月04日 Update

This article mainly shows you "what are the common js regular expressions", the content is simple and clear, and I hope it can help you solve your doubts, so let me lead you to study and learn about "what are the common js regular expressions".

Regular expressions are usually used for two tasks: 1. Verification, 2. Search / replace. When used for validation, you usually need to add ^ and $before and after to match the entire string to be verified; whether or not to add this qualification to search / replace depends on the requirements of the search, and it is also possible to add\ b before and after\ b instead of ^ and $. The common regular expressions listed in this table, except for a few, are not preceded by any restrictions. Please handle them yourself as needed.

1 user name regularization

/ / user name is regular, 4 to 16 digits (letters, numbers, underscores, minus signs) var uPattern = / ^ [a-zA-Z0-9 letters -] {4pm 16} $/; / output trueconsole.log (uPattern.test ("iFat3"))

2 password strength regularity

/ / password strength is regular, at least 6 characters, including at least 1 uppercase letter, 1 lowercase letter, 1 number, 1 special character var pPattern = / ^. * (? =. {6,}) (? =. *\ d) (? =. * [Amurz]) (? =. * [Amurz]) (?) (? =. * [! @ # $% ^ & *?]). / / output trueconsole.log ("=" + pPattern.test ("iFat3#"))

3 integer regularity

/ / positive integer regular var posPattern = / ^\ dhammer / negative integer regular var negPattern = / ^ -\ dhammer / integer regular var intPattern = / ^ -?\ dhammer / output trueconsole.log (posPattern.test ("42")); / / output trueconsole.log (negPattern.test ("- 42")); / / output trueconsole.log ("- 42"))

4 numeric regularity

It can be an integer or a floating point number.

/ / positive regular var posPattern = / ^\ d *\.?\ negative regular var negPattern = / ^ -\ d *\.??\ number regular var numPattern = / ^ -?\ d *\.?); console.log (negPattern.test ("- 42.2")); console.log (numPattern.test ("- 42.2"))

5 Email regular

/ / Email regular var ePattern = / ^ ([A-Za-z0-9 _\ -\.]) + @ ([A-Za-z0-9 _\ -\.]) +\. ([A-Za-z] {2jin4}) $/; / output trueconsole.log (ePattern.test (65974040@qq.com))

Or use

^\ w+ ([- +.]\ w+) * @\ w+ ([-.]\ w+) *.\ w+ ([-.]\ w+) * $

After testing, the matching degree is good.

6 regular mobile phone number

/ / Mobile phone number regular var mPattern = / ^ (13 [0-9]) | (14 [5 | 7]) | (15 ([0-3] | [5-9])) | (18 [0prime5-9]))\ d {8} $/; / output trueconsole.log (mPattern.test ("18600000000"))

7 ID card number regular

/ / ID card number (18 digits) 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 digits) 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 regularity

/ / 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

/ / regular date, simple judgment, var dP1 = / ^\ d {4} (\ -)\ d {1Magne2}\ 1\ d {1Magne2} $/; / output trueconsole.log (dP1.test ("2017-05-11")); / / output trueconsole.log (dP1.test ("2017-15-11")) / / date regularity Complexity decision 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) $/ / / output trueconsole.log (dP2.test ("2017-02-11")); / / output falseconsole.log (dP2.test ("2017-15-11")); / / output falseconsole.log (dP2.test ("2017-02-29"))

12 QQ number regular

/ / QQ sign regular, 5 to 11 digits var qqPattern = / ^ [1-9] [0-9] {4pm 10} $/; / output trueconsole.log (qqPattern.test ("65974040"))

13 Wechat regular number

/ / WeChat account regular, 6 to 20 digits, starting with a letter, letter, number, minus sign, underscore var wxPattern = / ^ [a-zA-Z] ([- _ a-zA-Z0-9] {5pm 19}) + $/; / output trueconsole.log (wxPattern.test ("RuilongMao"))

14 the license plate number is regular

/ license plate number regular var cPattern = / ^ [Beijing, Tianjin, Shanghai, Chongqing, Hebei, Henan, Yunnan, Hunan, Anhui, Shandong, new Jiangsu, Zhejiang, Jiangxi, Hubei, Guangxi, Ganjin, Shan, Ji, Fujian, Gui, Guangdong, Qinghai, Tibet, Sichuan and Ningqiong] {1} [A-Z0-9] {4} [A-Z0-9 hang academic police Hong Kong and Macao] {1} $/; / output trueconsole.log ("Beijing K39006")

15 contains Chinese rules

/ / contains Chinese regular var cnPattern = / [\ u4E00 -\ u9FA5] /; / / outputs trueconsole.log (cnPattern.test ("42 degrees"). This is all the content of the article "what are the common js regular expressions?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Regular output expression letter number integer common use date content article certificate number identity search verification underscore signal minus sign number address character Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Linux Huawei NVidia Apple