Many web developers can work smoothly after ignoring regular expressions, but in many cases a correct regular expression is likely to omit the half-screen code.
-
- The interpretation of regular expressions
- Terminology and operators
- Match a class of characters
- Interpretation
- Repeated occurrences
- Predefined character classes
- Group
- Or operator
- Reverse reference
- Compiling regular expressions
- Capturing a matched fragment
The interpretation of regular expressions
In JavaScript, as with most other object types, there are two ways to create regular expressions: through regular expression literals, or by constructing an instance of a RegExp object.
For example, if you want to create a generic regular expression that is used to precisely match the character creation "Test", you can use regular literals:
var pattern=/test/;
The forward slash may look strange, but just as the character a is defined in quotation marks, the regular literal is defined by a forward slash.
Alternatively, we can construct a regexp instance, passing the regular expression as a string:
var pattern=new RegExp("test");
In the development process, if the regular is known, the literal syntax is preferred, and the constructor is used to build the regular expression at run time by dynamically building the string. One of the reasons that literal syntax takes precedence over a string is that the backslash character is also an escape character in the normal string, and we want to use \ \ to represent the backslash.
In addition to the expression itself, there are three flags that can be associated with a regular:
I: Make regular expressions case-insensitive.
G: Matches all instances in the pattern, rather than the default matches only the first occurrence of the result.
M: Allows multiple lines to be matched.
These flags are appended to the literal tail (/test/ig) or as the second character parameter of the RegExp.
Terms and operators match a class of characters
[ABC]: matches any one of the characters in the a,b,c.
[^ABC]: matches any character except A,b,c.
[A-m]: matches all characters from A to M.
Interpretation
Repeated occurrences
/t?est/: the character T can occur 0 or 1 times
/t+est/: the character t can occur 1 or more times
/t*est/: the character T can occur 0 or more times
/a{4}/: This character appears 4 times
/a{4,10}/: Contains a string of 4 to 10 a characters in a row
These repeating operators can be greedy or non-greedy, which is greedy by default : matches all combinations of characters . Add a question mark behind the operator? character, such as A +, can make the expression non-greedy: make a minimum match .
Predefined character classes
pre-defined terms |
Match Content |
\ t |
Horizontal tab |
\b |
Space |
\v |
Vertical tab |
\f |
Page break |
\ r |
Enter |
\ n |
Line break |
. |
Matches any character except a new line |
\d |
Match any number, equivalent to [0~9] |
\d |
Match any non-digital, pricing with [^0~9] |
\w |
Match any word character that includes an underscore, equivalent to [a-za-z0-9_] |
\w |
Matches any non-word character, equivalent to [^a-za-z0-9_] |
\s |
Matches any whitespace character, including spaces, tabs, page breaks, etc. |
\s |
Match any non-whitespace character |
\b |
Match word boundaries |
\b |
Match non-word boundaries |
Group
If you apply an operator to a set of terms, you can use parentheses on the reorganization like a mathematical expression . For example:/(AB) +/matches one or more contiguous substrings "AB"
Or operator
A relationship that can be represented by a | character. e.g./(AB) +| (CD) +/matches the "AB" or "CD" that appears one or more times.
Reverse reference
This term notation is followed by a trailing slash and a number of captures to be referenced, starting with 1, such as \1, \2, and so on.
For example:/^ ([DTN]) a\1/matches either a d,t or N, followed by a a character, and followed by a string that captures the same character as the first one. Therefore \1 matching characters need to be determined at the time of execution.
It is useful when matching markup elements of XML types, such as/< (\w+) > (. +) </\1>/, which can match elements such as "<strong>shtat</strong>".
Compiling regular expressions
Two important stages of a regular expression are compilation and execution, and compilation occurs when a regular expression is first created, and execution occurs when we use a compiled regular expression for string matching.
It is an important optimization process to create a regular expression once and save it in a variable for later use.
Capturing a matched fragment
A simple match between a character hit and a pattern is obviously the first step we need to make, but in many cases it is also useful to determine what is matched.
Http://www.cnblogs.com/depsi/p/5163848.html
Break the hard-bitten bone-regular expression (go)