You can write less than 1000 lines of code if you know the 20 regular expressions.

Source: Internet
Author: User

You can write less than 1000 lines of code if you know the 20 regular expressions.

Regular Expressions, a very old and powerful text processing tool, can quickly implement a very complex business logic by simply using a very short expression statement. Mastering regular expressions can greatly improve your development efficiency. The following are the 20 regular expressions that are frequently used in front-end development.

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 {}) (-| \/) (\ d {}) \ 2 (\ d {}) $ /;

Var r = fieldValue. match (reg );

If (r = null) alert ('date format error! ');

 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.

^ (? =. * \ D )(? =. * [A-z]) (? =. * [A-Z]). {8, 10} $

2. Verify Chinese Characters

The string can only be Chinese.

^ [\ 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.

[\ 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:

^ [1-9] \ d {7} (0 \ d) | (1 [0-2]) ([0 | 1 | 2] \ d) | 3 [0-1]) \ d {3} $

18 digits:

^ [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.

^ (? :(?! (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. (According to the current domestic collection number, expand the first two numbers)

^ (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.

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

  10. Verify IP-v4 address

IP4 regular statement.

\ 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.

(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){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}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){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,1}[0-9]){0,1}[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])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[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.

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

  14. File Path and extension Verification

Verify the file names and extensions under Windows (The following example is the. txt file)

^ ([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-f0-9] {6} | [A-Fa-f0-9] {3}) $

  16. Extract webpage Images

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

  \ <* [Img] [^ \>] * [src] * = * [\ "\ '] {0, 1} ([^ \" \' \>] *)

 17. Extract the page hyperlink

Extracts hyperlinks from html.

(<A \ s *(?!. * \ Brel =) [^>] *) (href = "https? :\/\/)((?! (? :(? : Www \.)? '. Implode (' | (? : Www \.)? ', $ Follow_list).') [^ "] + )"((?!. * \ Brel =) [^>] *) (? : [^>] *)>

 18. Search for CSS attributes

The following expression can be used to search for matching CSS attributes.

^ \ 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 the tag attribute in HTML.

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

Regular expression syntax

Below is a very good Regular Expression Cheat Sheet I found, which can be used to quickly find related syntaxes.

  Learn Regular Expressions

I have seen a fairly good quick Learning Guide for regular expressions on the Internet. If you are interested in further study, you can refer to it.

 Online Testing Tool for Regular Expressions

Regex101 is a very good online testing tool for regular expressions. You can test your regular expressions online.

If you have added a useful regular expression to your favorites, you can also share it in your comments.

Author's Public Account:

 

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.