You can write less regular expressions of 1000 lines of code and 1000 rows of regular expressions.

Source: Internet
Author: User

You can write less regular expressions of 1000 lines of code and 1000 rows of regular expressions.

Mastering regular expressions can greatly improve your development efficiency.

Regular Expressions are often used for field or any string verification. For example, the following section verifies the JavaScript code of 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!');

The following are the 20 regular expressions that are frequently used in front-end development:

1. Verify password strength
The password must be a combination of uppercase and lowercase letters and numbers. special characters are not allowed and the password length must be between 8 and 10.

Copy codeThe Code is as follows: ^ (? =. * \ D )(? =. * [A-z]) (? =. * [A-Z]). {8, 10} $

2. Verify Chinese Characters
The string can only be Chinese.

Copy codeThe Code is as follows: ^ [\ u4e00-\ u9fa5] {0,} $

3. A string consisting of digits, 26 English letters, or underscores

^ \ W + $

4. Verify the email address
Like passwords, the following is a regular check statement for E-mail address compliance.

Copy codeThe Code is as follows: [\ w! # $ % & '* +/=? ^ _ '{| }~ -] + (? : \. [\ W! # $ % & '* +/=? ^ _ '{| }~ -] + )*@(? : [\ W] (? : [\ W-] * [\ w])? \.) + [\ W] (? : [\ W-] * [\ w])?

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

15 digits:Copy codeThe Code is as follows: ^ [1-9] \ d {7} (0 \ d) | (1 [0-2]) ([0 | 1 | 2] \ d) | 3 [0-1]) \ d {3} $

18 digits:Copy codeThe Code is as follows: ^ [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) $

6. Verification date
The date verification in the format of "yyyy-mm-dd" has been taken into consideration.

Copy codeThe Code is as follows: ^ (? :(?! (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) $

7. Verify the amount
Amount verification, accurate to 2 decimal places.

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

8. Verify the mobile phone number
The following is a regular expression for mobile phone numbers starting with "13", "15", and "18" in China.

Copy codeThe Code is as follows: ^ (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} $

9. Determine the IE version
IE is not completely replaced yet. Many pages still need to be compatible with the version. The following is the expression for IE version check.

Copy codeThe Code is as follows: ^. * MSIE [5-8] (? : \. [0-9] + )? (?!. * Trident \/[5-9] \. 0). * $

10. Verify IP-v4 address
IP4 regular statement.

Copy codeThe Code is as follows: \ B (? :(? : 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]?) \ B

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

Copy codeThe Code is as follows: ([0-9a-fA-F] {} :) {} [0-9a-fA-F] {} | ([0-9a-fA-F }: | ([0-9a-fA-F] {} :) {}: [0-9a-fA-F] {} | ([0-9a-fA-F] {} :) {} (: [0-9a-fA-F }) {1, 2} | ([0-9a-fA-F] {1, 4} :) {1, 4} (: [0-9a-fA-F] {1, 4 }) {1, 3} | ([0-9a-fA-F] {1, 4} :) {1, 3} (: [0-9a-fA-F] {1, 4 }) {} | ([0-9a-fA-F] {} :) {} (: [0-9a-fA-F] {}) {} | [0-9a-fA-F }:((: [0-9a-fA-F] {}) | :( (: [0-9a-fA-F] {}) {} |:) | fe80 :(: [0-9a-fA-F }) {} % [0-9a-zA-Z] {1, }|:: (ffff (: 0 }) {0, 1} :) {0, 1} (25 [0-5] | (2 [0-4] | 1 {0, 1} [0-9]) {0, 1} [0-9]) \.) {3, 3} (25 [0-5] | (2 [0-4] | 1 {} [0-9]) {} [0-9]) | ([0-9a-fA-F] {1, 4} :) {1, 4} :( (25 [0-5] | (2 [0-4] | 1 {0, 1} [0-9]) {0, 1} [0-9]) \.) {} (25 [0-5] | (2 [0-4] | 1 {} [0-9]) {} [0-9])

12. Check the URL prefix.
In application development, it is often necessary to distinguish whether the request is HTTPS or HTTP. The following expression can be used to retrieve the prefix of a url and then perform logic judgment.

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

13. Extract URL links
The following expression filters out the URLs in a piece of text.

Copy codeThe Code is as follows: ^ (f | ht) {1} (tp | tps): \/([\ w-] + \\.) + [\ w-] + (\/[\ w -. /? % & =] *)?

14. File Path and extension Verification
Verify the file path and extension

Copy codeThe Code is as follows: ([a-zA-Z] \\:|\\\\) \\\\ ([^\\\] + \\\\) * [^ \\/:*? "<> |] + \. Txt (l )? $

15. extract Color Hex Codes
You can use the following expression to extract the color code from a webpage.

\\# ([A-fA-F] | [0-9]) {3, 6}

16. Extract webpage Images
If you want to extract all the image information on the webpage, you can use the following expression.

Copy codeThe Code is as follows: \ <* [img] [^ \>] * [src] * = * [\ "\ '] {0, 1} ([^ \\"\\' \>] *)

17. Extract the page hyperlink
Extracts hyperlinks from html.

Copy codeThe Code is as follows: (<; a \ s *(?!. * \ Brel =) [^>;] *) (href = "https? ://)((?! (? :(? : Www \\.)? '. Implode (' | (? : Www \\.)? ', $ Follow_list).') [^ "] + )"((?!. * \ Brel =) [^>;] *) (? : [^>;] *)>

18. Refine CSS
The following expression can be used to search for CSS with the same attribute value to refine the code.

Copy codeThe Code is as follows: ^ \ s * [a-zA-Z \-] + \ s * [:] {1} \ s [a-zA-Z0-9 \ s. #] + [;] {1}

19. Extract comments
If you need to remove the comment in HMTL, you can use the following expression.

<! --(.*?) -->

20. Matching HTML tags
The following expression can match tags in HTML.

Copy codeThe Code is as follows: </? \ W + (\ s + \ w + (\ s * = \ s *(? :".*? "| '.*? '| [\ ^' "> \ S] + ))?) + \ S * | \ s *)/?>

You can write 20 regular expressions with less than 1000 lines of code. Come and learn it!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.