In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
JavaScript regular expression assembly? Many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can gain something.
2.1 user name regular 2.1.1 basic user name regular
User name regular check is used when user registration is done.
The basic user name naming rules are as follows:
A minimum of 4 bits, a maximum of 16 bits {4jin16}
Can contain lowercase capital letters [amurz] and uppercase letters [Amurz]
Can contain numbers [0-9]
Can include underscore [_] and minus sign [-]
The first letter can only be uppercase and lowercase
Var pattern = / ^ [a-zA-Z] [a-zA-Z0-9pm -] {3pc15} $/; / output trueconsole.log ("ifat3:" + pattern.test ('ifat3')); / / output trueconsole.log ("Ifat3:" + pattern.test (' Ifat3')); / / output trueconsole.log ("ke30:" + pattern.test ('ke30')); / / output falseconsole.log ("30ke:" + pattern.test (' 30ke')) / / output falseconsole.log ("ke3:" + pattern.test ('ke3')); output falseconsole.log ("ke30@:" + pattern.test (' ke30@')); / / output falseconsole.log ("ke30ke30ke30ke30ke30:" + pattern.test ('ke30ke30ke30ke30ke30')); 2.1.2 regular Chinese user name
If the Chinese user name is allowed in the rule, the regular expression is changed as follows:
Var pattern = / ^ [a-zA-Z\ u4E00 -\ u9FA5] [a-zA-Z0-9\ u4E00 -\ u9FA5lesson -] {3Ifat3' 15} $/ / output trueconsole.log ("ifat3:" + pattern.test ('ifat3')); / / output trueconsole.log ("Ifat3:" + pattern.test (' Ifat3')); / / output trueconsole.log ("30 lessons Maori:" + pattern.test ('Thirty lessons Maori')) / / output falseconsole.log ("30ke:" + pattern.test ('30ke')); / / output falseconsole.log ("ke3:" + pattern.test (' ke3')); / / output falseconsole.log ("ke30@:" + pattern.test ('ke30@')); / / output falseconsole.log ("ke30ke30ke30ke30ke30:" + pattern.test (' ke30ke30ke30ke30ke30'))
Among them, [\ u4E00 -\ u9FA5] is the regular matching of Chinese characters, including more than 20, 000 basic Chinese characters, in which\ u4E00 represents the Chinese character "one". For more information, please see "unicode coding range of Chinese characters".
2.2 password strength regular / / password strength regular, at least 6 digits, including at least 1 uppercase letter, 1 lowercase letter, 1 number, 1 special character var pPattern = / ^. * (? =. {6,}) (? =. *\ d) (? =. * [Amurz]) (? =. * [Amurz]) (? =. * [! @ # $% ^ & *?). / / output trueconsole.log ("iFat3#:" + pPattern.test ("iFat3#"))
The above regular expressions can only determine the basic passability of the user's password strength. For more information on password strength verification, see: analysis and implementation of password strength detection algorithm based on rule scoring.
2.3Numeric correlation regular 2.3.1 Integer regular / / positive integer regular var posPattern = / ^\ negPattern.test / negative integer regular var negPattern = / ^ -\ dhammer / integer regular var intPattern = / ^ -?\ dhammer / output trueconsole.log ("30:" + posPattern.test ("30")); / / output trueconsole.log ("- 30:" + negPattern.test ("- 30")) / / output trueconsole.log ("- 30:" + intPattern.test ("- 30")); 2.3.2 floating-point regular / / positive floating-point regular var posPattern = / ^\ d *\. The positive floating-point regular var negPattern = / ^ -\ d *\. The negative floating-point regular var twoPattern = / ^ -\ d *\. / / output trueconsole.log ("30.2:" + posPattern.test ("30.2"); / / output trueconsole.log ("- 30.2:" + negPattern.test ("- 30.2"); / / output trueconsole.log ("- 30.22:" + twoPattern.test ("- 30.22")); 2.3.3 regular integer floating point numbers
It can be an integer or a floating point number.
/ / positive regular var posPattern = / ^\ d *\.?\ negative regular var negPattern = / ^ -\ d *\.?\ digital regular var numPattern = / ^ -?\ d *\.?); / / output trueconsole.log ("- 30.2:" + negPattern.test ("- 30.2")) / / output true console.log ("- 30.2:" + numPattern.test ("- 30.2")) 2.4 date regular 2.4.1 date of birth var pattern = / ^ (19 [2-9]\ d {1}) | (20 ((0 [0-9]) | (1 [0-8])\-(0? [1-9])\-(0? [1-9]))\-(0? [1-9]) | ([1-2] [0-9]) | 30 | 31) $/ / output trueconsole.log (pattern.test ("1923-3-18")); / output trueconsole.log (pattern.test ("1923-4-31")); / / output trueconsole.log (pattern.test ("1923-2-29")); / / output trueconsole.log (pattern.test ("2016-2-29"))
The above regular verification is not perfect, mainly due to the number of days in November.
2.4.2 General date regular / / date regular 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")) 2.5 Email regular 2.5.1 basic Email regular var pattern = / ^ ([A-Za-z0-9 _\ -\.]) + @ ([A-Za-z0-9 _\ -\.]) +\. ([A-Za-z] {2pr 4}) $/; / output trueconsole.log (pattern.test ('cn30ke@163.com')); / / output trueconsole.log (pattern.test (' ifat3@sina.com.cn')) / / output trueconsole.log (pattern.test ('ifat3.it@163.com')); / / output trueconsole.log (pattern.test (' ifat3_-.@30ke.cn')); / / output falseconsole.log (pattern.test ('ifat3@30ke.online')); / / output falseconsole.log (' Maori @ 30ke.cn')
Basic Email regularity is the most commonly used verification method, and it is also suitable for most application scenarios. As you can see from the above tests, this expression does not support domain names ending with .online and .store. If you need to be compatible with this kind of domain name (more than 4 digits), you can adjust the restriction part of the regular ending {2jin4} (for example: {2jing8}). Another problem is that the Email user name cannot include Chinese.
2.5.2 Chinese name Email regular
According to the question in the previous rule, two additional rules are as follows:
User name can include Chinese [\ u4e00 -\ u9fa5]
The domain name ends with a maximum of 8 digits {2pm 8}.
Var pattern = / ^ ([A-Za-z0-9 _\ -\. U4e00 -\ u9fa5]) + @ ([A-Za-z0-9 _\ -\.]) +\. ([A-Za-z] {2mer8}) $/; / output trueconsole.log (pattern.test ('cn30ke@163.com')); / / output trueconsole.log (pattern.test (' ifat3@sina.com.cn')) / / output trueconsole.log (pattern.test ('ifat3.it@163.com')); / / output trueconsole.log (pattern.test (' ifat3_-.@30ke.cn')); / / output trueconsole.log (pattern.test ('ifat3@30ke.online')); / / output trueconsole.log (pattern.test (' Maori @ 30ke.cn')); 2.5.3 Email regular for specific domain names
Before the emergence of mobile CAPTCHA, almost mailbox verification was the only condition to ensure the uniqueness of users. The emergence of temporary mailboxes (also known as 10-minute mailboxes or disposable mailboxes) makes the mechanism of mailbox verification and account activation meaningless. The address of the temporary mailbox can not be enumerated, we can only adopt the whitelist method, only a limited number of email domain names are allowed to pass verification.
Var pattern = / ^ ([A-Za-z0-9 _\ -\.]) +\ @ (163.com | qq.com | 30ke.cn) $/; / output trueconsole.log (pattern.test ('cn30ke@163.com')); / / output falseconsole.log (pattern.test (' ifat3@sina.com.cn')); / / output trueconsole.log (pattern.test ('ifat3.it@163.com')); / / output trueconsole.log (pattern.test (' ifat3_-.@30ke.cn')) / / output falseconsole.log (pattern.test ('ifat3@30ke.online')); / / output falseconsole.log (pattern.test (' Maori @ 30ke.cn'))
Although this method ensures authentication security, if the whitelist is too long, the pattern string will be too long. At this time, you can write the whitelist of the email domain name into an array, use the regular expression to do the preliminary verification, and use the whitelist to do the second verification of the domain name.
Whitelist array of commonly used domain names:
Var domains= ["qq.com", "163.com", "vip.163.com", "263.net", "yeah.net", "sohu.com", "sina.cn", "sina.com", "eyou.com", "gmail.com", "hotmail.com"]
The above whitelist lists only 11 commonly used email domain names, which can be added or deleted as needed.
2.6 regular Mobile number / / regular Mobile number var mPattern = / ^ ((13 [0-9])) | (14 [5 | 7]) | (15 ([0-3] | [5-9])) | (18 [0meme 5-9]))\ d {8} $/; / output trueconsole.log (mPattern.test ("18600000000")) 2.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 | 31)\ d {3} [0-9Xx] $/; / output trueconsole.log (cP.test ("11010519880605371X"))
The above rules can only make a basic decision on the passability of the ID card number. For more information on the determination of citizenship number, please refer to the document: determination of the correctness of citizenship number and program implementation.
2.8 URL regular / / URL regular var urlP= / ^ ((https? | ftp | file):\ /\ /)? ([\ da-z\. -] +)\. ([a murz\.] {2jue 6}) ([\ /\ w\. -] *) *\ /? $/; / output trueconsole.log (urlP.test (http://30ke.cn));) 2.9 IP address 2.9.1 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")) 2.9.2 IPv6 address regular / / IPV6 regular var pattern = / (([0-9a-fA-F] {1 9a-fA-F 4}:) {7 9a-fA-F 7} [0-9a-fA-F] {1 9a-fA-F 4}:) {1 9a-fA-F 7}:) {1 9a-fA-F 7}:) {1 9a-fA-F 6}: [0-9a-fA-F] {1pm 4}:) {1 Magi 4}:) {1 Magi 4}:) } (: [0-9a-fA-F] {1Power4}) {19a-fA-F 2} | ([0-9a-fA-F] {1Power4}:) {1 4} (: [0-9a-fA-F] {1Power4}) {1 3} | ([0-9a-fA-F] {1pr 4}:) {1jue 3} (: [0-9a-fA-F] {1pr 4}) {1jue 4} | ([0-9a-fA-F] {1jue 4}:) {1jue 2} (: [0-9a-fA]) -F] {19a-fA-F 4}) {1fe80 5} | [0-9a-fA-F] {1par 4}: ((: [0-9a-fA-F] {1pr 4}) {1pr 6}) | (: (: [0-9a-fA-F] {1pr 4}) {1pr 7} |:) | fe80: (: [0-9a-fA-F] {0jue 4}) {0jue 4}% [0-9a-zA-Z] {1} | |:: (ffff (: 0 {0 9a-fA-F 4}) {0rect 1}:) {0 9a-fA-F 1} ((25 [0-5] | (2 [0-4] | 1 {0rect 1} [0-9]) {0Magne} [0-9])\.) {3pm 3} (25 [0-5] | (2 [0-4] | 1 {0rect 1} [0-9]) {0jue 1} [0-9]) | ([0-0] 1}:) {1pm 4}: (25 [0-5]) 0-5] | (2 [0-4] | 1 {0rect 1} [0-9]) {0rect 1} [0-9])\.) {3jue 3} (25 [0-5] | (2 [0-4] | 1 {0jue 1} [0-9])) / / / output trueconsole.log (pattern.test ("fe80:0000:0000:0000:0204:61ff:fe9d:f156")); 2.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")) 2.11 QQ number regular / / QQ number regular, 5 to 11 digits var qqPattern = / ^ [1-9] [0-9] {4 10} $/; / output trueconsole.log (qqPattern.test ("65974040")); 2.12 Wechat regular / / WeChat regular, 6 to 20 digits, beginning 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")); 2.13license plate number regular / / license plate number regular var cPattern = / ^ [Beijing, Tianjin, Shanghai, Chongqing, Hebei, Henan, Yunnan, Liaoning, black, Shandong, new Jiangsu, Zhejiang, Jiangxi, Hubei, Guangxi, Shanxi, Shaanxi, Fujian, Gui, Guangdong, Qinghai, Tibet, Sichuan, Ningqiong envoy Amurz] {1} [A-Z0-9] {4} [A-Z0-9 students, policemen, Hong Kong and Macao] {1} $/ / / output trueconsole.log (cPattern.test ("Beijing K39006")); 2.14 contains Chinese regular var cnPattern = / [\ u4E00 -\ u9FA5] /; / / output trueconsole.log (cnPattern.test ("30 lessons")); is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.