20 regular expressions are required (you can write less than 1,000 lines of code), and rows of Regular Expressions

Source: Internet
Author: User

20 regular expressions are required (you can write less than 1,000 lines of code), and rows of Regular Expressions

A regular expression (regular expression) describes a string matching pattern, it can be used to check whether a string contains a seed string, replace matched substrings, or retrieve substrings that meet certain conditions from a string.

In the column directory, *. txt in dir *. txt or ls *. txt is not a regular expression, because here * is different from the regular expression.

The method for constructing a regular expression is the same as that for creating a mathematical expression. That is to say, using a variety of metacharacters and operators can combine small expressions to create larger expressions. The regular expression component can be a single character, Character Set combination, character range, choice between characters, or any combination of all these components.

A regular expression is a text format consisting of common characters (such as characters a to z) and special characters (called metacharacters. Mode description one or more strings to be matched when searching text. A regular expression is used as a template to match a character pattern with the searched string.

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.

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.

^(?=.*\\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[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.

^(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 path and extension

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

\\< *[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. Refine CSS

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

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

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

These 20 regular expressions can help you write less than 1,000 lines of code. For more information, see!

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.