Basic concepts
A regular expression is a text pattern that includes normal characters (for example, letters between a to z) and special characters (called "metachars"). The pattern describes one or more strings to match when searching for text.
First, several regular expression editors are recommended
Debuggex: https://www.debuggex.com/
PyRegex: http://www.pyregex.com/
Regexper: http://www.regexper.com/
Regular expressions are search and string replacement operations. Regular expressions are widely used in text editors, such as regular expressions are used for:
[copy] Check whether the text contains the specified characteristic word
Find out where matching characteristic words are located in the text
Extract information from text, such as: substrings of strings
Modify text
Note: Regular expressions are usually used for two tasks: 1. Verification, 2. Search/Replace. When used for verification, it is usually necessary to add ^ and $ before and after to match the entire string to be verified; whether to add this limitation when searching/replacement depends on the search requirements. In addition, it is also possible to add /b instead of ^ and $ before and after. Except for a few, no restrictions are added before and after the common regular expressions listed in this table. Please handle them yourself as needed.
Priority order
After constructing a regular expression, you can evaluate like a mathematical expression, that is, you can evaluate from left to right and in a priority order. The following table lists the priority order of various regular expression operators from the highest priority to the lowest priority:
| Operator | describe |
|---|---|
| / | Escape symbol |
| (), (?:), (?=), [] | Braces and square brackets |
| *, +, ?, {n}, {n,}, {n,m} | Qualifier |
| ^, $, /anymetacharacter | Position and order |
Create regular expressions
The method of constructing regular expressions is the same as the method of creating mathematical expressions. That is, use multiple metacharacters and operators to combine small expressions to create larger expressions.
A regular expression can be constructed by putting various components of the expression pattern between a pair of separators.
For JScript, the delimiter is a pair of forward slashes (/) characters. For example:
/expression/
For VBScript, a pair of quotes ("") is used to determine the boundaries of the regular expression. For example:
The code copy is as follows:
"expression"
See an example
The code copy is as follows:
//Match whether the account is legal (beginning with letters, 5-16 bytes allowed, alphanumeric underscores allowed
var re =new RegExp("^[a-zA-Z][a-zA-Z0-9_]{5,19}$");
if(re.test(aaaa)){
alert("correct format");
}else{
alert("format error");
}
Components of regular expressions can be a single character, a set of characters, a range of characters, a choice between characters, or any combination of all of these components.
Commonly used regular expressions
Regular expression matching Chinese characters: [/u4e00-/u9fa5]
Comment: Matching Chinese is really a headache, it's easy to do with this expression
Match double-byte characters (including Chinese characters): [^/x00-/xff]
Comment: It can be used to calculate the length of a string (a double-byte character length meter 2, ASCII character meter 1)
Regular expression matching blank lines: /n/s*/r
Comment: Can be used to delete blank lines
Regular expression matching HTML tags: <(/S*?)[^>]*>.*?<//1>|<.*? />
Comment: The version circulating online is too bad, and the above one can only match the part, and it is still powerless to use complex nested markers.
Regular expression matching the beginning and end whitespace characters: ^/s*|/s*$
Comment: It can be used to delete whitespace characters at the beginning and end of the line (including spaces, tabs, page breaks, etc.), a very useful expression
Regular expression matching the email address: /w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*
Comment: It is very practical when verifying the form
Regular expression matching URL: [a-zA-z]+://[^/s]*
Comment: The functions of the version circulating online are very limited, and the above can basically meet the needs
Match whether the account is legal (beginning with letters, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Comment: It is very practical when verifying the form
Match domestic phone number: /d{3}-/d{8}|/d{4}-/d{7}
Comment: Matching forms are as follows: 0511-4405222 or 021-87888822
Match Tencent QQ number: [1-9][0-9]{4,}
Comment: Tencent QQ number starts at 10,000
Match the Chinese postal code: [1-9]/d{5}(?!/d)
Comment: China's postal code is 6 digits
Match ID card: /d{15}|/d{18}
Comment: China's ID card is 15 or 18 digits
Match IP address: /d+/./d+/./d+/./d+
Comment: It is useful when extracting IP addresses
Match specific numbers
[copy] ^[1-9]/d*$ //Match positive integers
^-[1-9]/d*$ //Match negative integers
^-?[1-9]/d*$ //Match integers
^[1-9]/d*|0$ //Match non-negative integers (positive integer + 0)
^-[1-9]/d*|0$ //Match non-positive integers (negative integers + 0)
^[1-9]/d*/./d*|0/./d*[1-9]/d*$ //Match positive floating point numbers
^-([1-9]/d*/./d*|0/./d*[1-9]/d*)$ //Match negative floating point numbers
^-?([1-9]/d*/./d*|0/./d*[1-9]/d*|0?/.0+|0)$ //Match floating point numbers
^[1-9]/d*/./d*|0/./d*[1-9]/d*|0?/.0+|0$ //Match non-negative floating point numbers (positive floating point numbers + 0)
^(-([1-9]/d*/./d*|0/./d*[1-9]/d*))|0?/.0+|0$ //Match non-positive floating point numbers (negative floating point numbers + 0)
Comment: It is useful when processing large amounts of data, and be careful to correct it when applying it in detail.
Match a specific string
[copy]^[A-Za-z]+$ //Match a string composed of 26 English letters
^[AZ]+$ //Match a string composed of 26 English letters capitalizations
^[az]+$ //Match a string composed of 26 English letters lowercase
^[A-Za-z0-9]+$ //Match a string composed of numbers and 26 English letters
^/w+$ //Match a string composed of numbers, 26 English letters or underscores
Comment: Some of the most basic and most commonly used expressions
Mind Map