Defined
JavaScript regular expressions are defined in two ways, defining a string that matches similar <%XXX%>
1. Constructors
var reg=new RegExp (' <%[^%>]+%> ', ' G ');
2. Literal
var reg=/<%[^%>]%>/g;
- g: Global, Full text search, default search to first result stop
- I: ingore case, ignoring casing, default case sensitivity
- m: Multiple lines, multi-line search (change the meaning of ^ and $ so that they match the beginning and end of the line, respectively, on any line, not just at the beginning and ending of the entire string)
Metacharacters
The regular expression is prohibitive for one important reason is that its escape character is too many, the composition is very many, but the regular expression of the metacharacters (special meaning in the regular expression of private characters, can be used to specify its leading character) is not many
Metacharacters: ([{\ ^ $ |)? * +.
Not every meta-character has its specific meaning, in different combinations of meta-characters have different meanings, classification to see
Pre-defined special characters
| Character |
Meaning |
| \ t |
Horizontal tab |
| \ r |
Carriage return character |
| \ n |
Line break |
| \f |
Page break |
| \cX |
control character corresponding to X (ctrl+x) |
| \v |
Vertical tab |
| / |
Null character |
Character class
In general, a regular expression is a character (an escape character is counted one) that corresponds to a character in a string, and the meaning of the expression ab\t is
But we can use the meta-character [] to construct a simple class, the so-called class refers to the object that conforms to certain characteristics, is a generic, rather than a particular character, we can use the expression [ABC] to classify the character A or B or C as a class, the expression can match this kind of character
Metacharacters [] combination can create a class , we can also use the meta-character ^ to create a reverse class/negative class, reverse class meaning is not part of the content of the XXX class, the expression [^abc] means the content is not a character A or B or C
Scope class
According to the above instructions, if we want to match a single number, the expression is like this.
[0123456789]
If it's a letter then ... , good trouble, the regular expression also provides a range class, we can use X-y to connect two characters from X to y any character, this is a closed interval, that is, contains X and ybenshen, so matching lowercase letters is very simple
[A-z]
What if you want to match all the letters? Within the [] class, we can ligatures, and we can write [a-za-z]
Pre-defined Classes
Just using the regular we've created several classes to represent numbers, letters, etc., but it's also cumbersome to write, and regular expressions give us a few common predefined classes to match common characters
| Character |
Equivalence class |
Meaning |
| . |
[^\n\r] |
All characters except carriage return and line break |
| \d |
[0-9] |
numeric characters |
| \d |
[^0-9] |
Non-numeric characters |
| \s |
[\t\n\x0b\f\r] |
whitespace characters |
| \s |
[^ \t\n\x0b\f\r] |
Non-whitespace characters |
| \w |
[A-za-z_0-9] |
Word characters (Letters, numbers, underscores) |
| \w |
[^a-za-z_0-9] |
Non-word characters |
With these pre-defined classes, it is convenient to write some regular, such as we want to match a ab+ number + any character string, you can write the ab\d.
Boundary
Regular expressions also provide several commonly used boundary-matching characters
Character |
Meaning |
^ |
Start with XX |
$ |
End With XX |
\b |
Word boundaries, meaning characters other than [a-za-z_0-9] |
\b |
Non-word boundary |
Look at an irresponsible mailbox regular match (do not imitate, the parentheses will talk about) \[email protected]\w+\. (COM) $
Quantifiers
The methods we introduced before are all one by one matches, if we want to match a string that has 20 consecutive numbers we need to write this
\d\d\d\d ...
Some quantifiers are introduced for this regular expression
| Character |
Meaning |
| ? |
Occurs 0 or more times (up to one time) |
| + |
One or more occurrences (at least once) |
| * |
Occurs 0 or more times (any time) |
| N |
Appears n times |
| {N,m} |
Occurs N to M times |
| {N,} |
appears at least n times |
|
|
See some examples of using quantifiers
\w+\b Byron match word + boundary +byron
(/\w+\b byron/). Test (' Hi Byron '); True (/\w+\b byron/). Test (' Welcome Byron '); True (/\w+\b byron/). Test (' Hibyron '); False
\d+\.\d{1,3} matches a number of three decimal places
Greedy mode and non-greedy mode
Read the above introduction of the quantifier, perhaps the classmate will think about the matching principle of some problems, such as {3,5} This quantifier, if in the sentence type appeared 10 times, then he is each match three or five, anyway 3, 4, 5 are satisfied with the conditions, quantifiers in the default is as many matching, That's what we call greedy patterns.
Since there is greedy mode, then there must be non-greedy mode, so that the regular expression as little as possible to match, that is, once the successful match does not stop the attempt, the practice is simple, after the quantifier plus? Can
' 123456789 '. Match (/\d{3,5}?/g); ["123", "456", "789"]
Group
Sometimes we want to use quantifiers to match more than one character, rather than just like the above example is a match, for example, want to match Byron appear 20 times the string, if we write byron{20} words match is byro+n appear 20 times, how to Byron as a whole? Use () can achieve sub-purpose, we call grouping
(Byron) {20}
What if you want to match Byron or Casper 20 times? can use characters | To achieve or to function
(byron| Casper) {20}
We see a # # of stuff in the picture, what's that? Regular expressions that use grouping will also put matches in groups, which are distributed by number numbers by default, capturing grouped content based on numbers, which is useful in some functions that wish to manipulate the first few matches.
(Byron). (OK)
If there is a case for grouping nesting, the outer group is numbered before
((^|%>) [^\t]*)
Sometimes we don't want to capture certain groupings, we just need to add them within the group: Yes, it doesn't mean that the grouped content doesn't belong to regular expressions, but it doesn't add numbers to the group.
(?: Byron). (OK)
In fact, in C # and other languages can also be a name group, but JavaScript does not support
Prospect
| An expression |
Meaning |
| EXP1 (? =exp2) |
The match is followed by the Exp2 exp1. |
| EXP1 (?! EXP2) |
Match the EXP1 that are not exp2 behind |
There is some abstraction to say, see an example good (? =byron)
(/good (? =byron)/). EXEC (' goodByron123 '); [' Good '] (/good (? =byron)/). EXEC (' goodCasper123 '); Null (/bad (? =byron)/). EXEC (' goodCasper123 ');//null
The above example shows that the EXP1 (? =exp2) expression will match the EXP1 expression, but only after it is EXP2, that is, two conditions, EXP1 (?!). EXP2) Compare Similar
Good (?! Byron)
(/good?! Byron)/). EXEC (' goodByron123 '); Null (/good?! Byron)/). EXEC (' goodCasper123 '); [' Good '] (/bad (?! Byron)/). EXEC (' goodCasper123 ');//null
Reference
Masaki JavaScript Regular Expressions
Regexper
PS: The diagram in the blog is done with a second link, helping people to understand the regular expression of the graphic, really good
JavaScript Regular Expressions-basic syntax-graphical representation understandable