Regular expressions, a very old and powerful text processing tool, can quickly implement a very complex business logic with only a very short expression statement. Mastering regular expressions can greatly improve your development efficiency.

Regular expressions are often used to validate fields or arbitrary strings, as in this JavaScript code that validates the basic date format:

Var/reg = ^ (\ \ d {1, 4}) (- | \ \ /) (\ \ d {1, 2}) \ \ 2 (\ \ d {1, 2}) $/; var r = fieldValue.match(reg); if(r==null)alert('Date format error! ');Copy the code

1. Verify the password strength

The password must contain a combination of uppercase and lowercase letters and numbers, and cannot contain special characters. The password must be between 8 and 10 characters in length.

^ (? =.*\\d)(? =.*[a-z])(? =. * [a-z]). 8, 10 {} $Copy the code

2. Verify that the Chinese character string can only be Chinese.

^[\\u4e00-\\u9fa5]{0,}$
Copy the code

3. The value contains 26 letters, digits, and underscores (_)

^\\w+$
Copy the code

4. Verify that the E-mail address is the same as the password. The following is a regular check statement for the E-mail address compliance.

[\\w!#$%&'*+/=?^_`{|}~-]+(? :\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(? :[\\w](? :[\\w-]*[\\w])? \ \.) +[\\w](? :[\\w-]*[\\w])?Copy the code

5. Verify the ID card number the following is the regular verification of the ID card number. 15 or 18.

15: ^ 1-9] [\ \ d {7} ((0 \ \ d) | (1) [0-2]) (([0 | 1 | 2] \ \ d) | 3 [0, 1]) \ \ d {3} $18:  ^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$Copy the code

6. Date verification Date verification in the format yyyY-MM-DD has been taken into account.

^ (? : (? ! 0000) [0-9] {4} - (? : (? : 0 | [1-9] [0-2] 1) - (? : 0 [1-9] [0-9] | | 1 2 [0 to 8]) | (? : 0 [9] 13 - | [0-2] 1) - (? 30) : 29 | | (? : 0 [13578] 1 [02]) - 31) | | (? : [0-9] {2} (? : 0 [48] | [2468] [048] | [13579] [26]) | (? : 0 [48] | [2468] [048] | [13579] [26]) 00) - 02 - $29)Copy the code

7. Check amount Check amount, accurate to 2 decimal places.

^ [0-9] + (. [0-9] {2})? $Copy the code

8. Verifying mobile phone numbers The following are the mobile phone numbers starting with 13, 15, and 18 in China. (The first two digits can be extended according to the current domestic collection number)

^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$
Copy the code

9. Determine the version of IE IE has not been completely replaced, many pages still need to do version compatibility, the following is the expression of the IE version check.

^.*MSIE [5-8](? : \ \. [0-9] +)? (? ! .*Trident\\/[5-9]\\.0).*$Copy the code

10. Verify the IP4 regular statement of IP-V4 address.

\\b(? : (? : 25 [0 to 5] | 2 [0 to 4] [0-9] | [01]? [0-9] [0-9]?) \ \.) {3} (? : 25 [0 to 5] | 2 [0 to 4] [0-9] | [01]? [0-9] [0-9]?) \\bCopy the code

11. Verify the IP6 regular statement of the IP-v6 address.

(([0-9 a - fA - F] {1, 4} {7, 7}) [0-9 a - fA - F] {1, 4} | ([0-9 a - fA - F] {1, 4} {1, 7}) : | ([0-9 a - fA - F] {1, 4} {1, 6}) : [0-9 a - fA - F] {1, 4} | ([0-9 - fA - a F] {1, 4} {1, 5}) (: [0-9 a - fA - F] {1, 4}, {1, 2} | ([0-9] a - fA - F {1, 4} {1, 4}) (: [0-9 a - fA - F] {1, 4}, {1, 3} | ([0-9 a - fA - F] {1, 4} {1, 3}) (: [0-9 - a FA - F] {1, 4} {1, 4} | ([0-9 a - fA - F] {1, 4}} {1, 2) (: [0-9 a - fA - F] {1, 4}, {1, 5} | [0-9 a - fA - F] {1, 4} : ((: [0-9 a - fA - F] {1, 4}, {1, 6}) | : ((: [0-9 - a FA - F] {1, 4} {1, 7} | :) | fe80: : [0-9 a - fA - F] {0, 4}) {0, 4} % [0-9 a zA - Z] {1} | : : (FFFF (: 0 {1, 4}) {0, 1} {0, 1} ((25) [0 to 5) | (2 [0 to 4] | 1 {0, 1} [0 9]) {0, 1} [0-9]) \ \.) {3} (25 [0 to 5) | (2 [0 to 4] | 1 {0, 1} [0-9]) {0, 1} [0-9]) | ([0-9 a - fA - F] {1, 4} {1, 4}) : ((25 [0 to 5) | (2 [0 to 4] | 1 {0, 1} [0-9]) {0, 1} [0-9]) \ \.) {3} (25 [0 to 5) | (2 | 1 [0-4] [0-9] {0, 1}) [0-9] {0, 1}))Copy the code

In application development, it is often necessary to distinguish between HTTPS and HTTP requests. The following expression can be used to extract a URL prefix and then determine logically.

if (! s.match(/^[a-zA-Z]+:\\/\\//)) { s = 'http://' + s; }Copy the code

This expression below the URL link filters out the URL in a piece of text.

^(f|ht){1}(tp|tps):\\/\\/([\\w-]+\\.) +[\\w-]+(\\/[\\w- ./?%&=]*)?Copy the code

14. File path and extension verification Verify the file path and extension in Windows (.txt file in the following example).

^([a-zA-Z]\\:|\\\\)\\\\([^\\\\]+\\\\)*[^\\/:*?"<>|]+\\.txt(l)? $Copy the code

Sometimes it is necessary to extract Color Codes from web pages. You can use the following expression.

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
Copy the code

If you want to extract all the image information in a web page, you can use the following expression.

\ \ < * [img] [^ \ \ \ \ >] * (SRC) * = * [\ \ "\ \ '] {0, 1} ([^ \ \ \ \ '\ \" >] *)Copy the code

Extract page hyperlinks Extract hyperlinks from HTML.

(<a\\s*(? ! .*\\brel=)[^>]*)(href="https? : \ \ / \ \ / ((?) ! (? : (? :www\\.) ? '.implode('|(? :www\\.) ? ', $follow_list).'))[^"]+)"((? ! .*\\brel=)[^>]*)(? : [^ >] *) >Copy the code

18. Search for CSS properties You can search for matching CSS properties using the following expression.

^\\s*[a-zA-Z\\-]+\\s*[:]{1}\\s[a-zA-Z0-9\\s.#]+[;] {1}Copy the code

If you need to remove comments from HMTL, you can use the following expression.

<! - (. *?) -->Copy the code

20. Matching HTML Tags You can match HTML tag attributes by using the following expression.

< \ \ /? \\w+((\\s+\\w+(\\s*=\\s*(? : ". *?" | '. *? '|[\\^'">\\s]+))?) +\\s*|\\s*)\\/? >Copy the code

The original author: mechanist links: www.jianshu.com/p/e7bb97218…