20 Regular expression must know (can let you write less 1,000 lines of code) _ Regular expression

Source: Internet
Author: User
Tags character set html tags regular expression

A regular expression (regular expression) describes a pattern of string matching that can be used to check whether a string contains a seed string, replaces a matching substring, or extracts a substring from a string that matches a condition.

Column directory, the *.txt in dir *.txt or LS *.txt is not a regular expression, because the meaning of this * is different from that of the regular type.

The method for constructing regular expressions is the same as for creating mathematical expressions. That is, multiple metacharacters and operators can combine small expressions to create larger expressions. The component of a regular expression can be a single character, character set, character range, selection between characters, or any combination of any of these components.

A regular expression is a literal pattern composed of ordinary characters, such as characters A through z, and special characters, called "metacharacters". The pattern describes one or more strings to match when searching for text. A regular expression is used as a template to match a character pattern with the string being searched for.

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

Regular expressions are often used for validation of fields or arbitrary strings, such as the following JavaScript code that validates the base date format:

var reg =/^ (\\d{1,4}) (-|\\/) (\\d{1,2}) \\2 (\\d{1,2}) $/; 
var r = Fieldvalue.match (reg);    

Here are 20 regular expressions that are frequently used in front-end development:

1. Verify Password strength

The strength of the password must be a combination of uppercase and lowercase letters and numbers, and cannot use special characters, which are between 8-10 lengths.

 
 

2. Check Chinese

The string can only be Chinese.

 
 

3. A string of numbers, 26 English letters, or underscores

 
 

4. Verify e-mail address

As with the password, the following is a regular check statement of the compliance of the e-mail address.

 
 

5. Verify ID Number

The following is a regular check of the ID number. 15 or 18 bits.

15-bit:

 

18-bit:

 
 

6. Date of verification

Date checksum in "YYYY-MM-DD" format has been considered for flat leap years.

^(?:(?! 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. Check Amount

Amount checksum, accurate to 2 decimal places.

 
 

8. Check the phone number

Below is the domestic 13, 15, 18 cell phone number regular expression.

 
 

9. To determine the version of IE

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

 
 

10. Verify IP-V4 Address

IP4 the 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 IP-V6 Address

IP6 the regular statement.

 
 

12. Check the prefix of the URL

Many times in application development it is necessary to distinguish between the request being HTTPS or HTTP, and the following expression can be used to remove the prefix of a URL and then logically judge it.

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

13. Extract URL link

The following expression filters the URL in a paragraph of text.

 
 

14. File path and extend the examination of famous schools

Verifying file paths and extensions

 
 

15. Extract Color Hex codes

Sometimes you need to extract the color code from a Web page, and you can use the following expression.

 
 

16. Extract Web Images

If you want to extract all the picture information from a Web page, you can use the following expression.

 
 

17. Extract Page Hyperlink

Extracts the hyperlinks in HTML.

 
 

18. Refine CSS

The following expression allows you to search for CSS of the same attribute value to refine your code.

 
 

19. Extract Notes

If you need to remove the annotation in HMTL, you can use the following expression.

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

20. Matching HTML tags

The following expression can be used to match the tags in HTML.

 
 

Really useful, master these 20 regular expressions can let you write less 1,000 lines of code, interested friends for reference under it!

Related syntax for regular expressions

Here is a very nice regular expression Cheat Sheet that I found that can be used to quickly find related syntax.

Learning Regular Expressions
I saw a fairly good regular expression on the Internet Learning Guide, interested in further study of students can refer to.

For beginners, yun-Habitat Community Small series recommended everyone see this article: http://www.jb51.net/tools/zhengze.html

Regular expression on-line test tool
Regex101 is a very good regular expression online testing tool, you can directly test your regular expression on the line Oh.

In addition, I also found some good regular expression tutorials and books on the Internet. You can go to the cloud habitat community to download.

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.