There are two ways that JavaScript defines regular expressions.
1.REGEXP Constructors
var pattern = new RegExp ("[Bc]at", "I");
It receives two parameters: one is the string pattern to match, and the other is the optional flag string.
2. Literal volume
var pattern =/[bc]at/i;
The matching pattern for regular expressions supports three kinds of flag strings:
G:global, the global search pattern, which will be applied to all strings, rather than searching for the first match to stop the search;
I:ingore case, ignoring letter capitalization, which ignores pattern and string capitalization when determining matches;
M:multiple lines, multiline mode, which continues to find whether the next row has a match when the search reaches the end of a line of text.
The difference between the two methods of creating regular expressions is that regular expression literals always share the same regexp instance, and each new RegExp instance created using the constructor is a new instance.
Metacharacters
Metacharacters are characters with special meanings, and the meta characters of regular expressions are mainly:
( [ { \ ^ $ | ) ? * + .
The meta characters have different meanings in different combinations.
Predefined special characters
Character class Simple Class
In general, a regular expression is a character that corresponds to a character string, but we can use [] to construct a simple class that represents a class of characters that conform to a particular feature. For example:
[ABC] can match A, B, C, or any combination of characters in square brackets.
Reverse class
Now that [] You can build a class, you naturally associate it with a class that does not contain the parentheses, which is called the reverse class, for example [^ABC] to match a character that is not a or B or C.
Scope class
Sometimes a character match is too troublesome and the matching type is the same, at which point we can use the "-" join line to represent the content between a closed interval, for example, to match all lowercase letters with [A-z], as follows:
Matching all 0 to 9 simply any number can be expressed using [0-9]:
Predefined classes
For some of the classes we've created, regular expressions provide us with several commonly used predefined classes to match common characters, as follows:
Character |
Equivalence class |
Meaning |
. |
[^\n\r] |
Matches all characters except carriage returns and line breaks |
\d |
[0-9] |
numeric characters |
\d |
[^0-9] |
Non-numeric characters |
\s |
[\t\n\x0b\f\r] |
White space characters |
\s |
[^\t\n\x0b\f\r] |
Non-whitespace characters |
\w |
[A-za-z_0-9] |
Word characters (Letters, numbers, and underscores) |
\w |
[^a-za-z_0-9] |
Non-word characters |
Quantifiers
The above method matching characters are one-to-one matching, if a character consecutive occurrences of the above method of matching will be very troublesome, so we think there are other ways to directly match multiple occurrences of characters. Regular expressions provide us with a number of quantifiers, as follows:
Character |
Meaning |
? |
appear 0 or one time (up to one time) |
+ |
appear one or more times (at least once) |
* |
Occurs 0 or more times (any time) |
N |
Occurs n times |
{N,m} |
Appears N to M times |
{N,} |
Occurs at least n times |
Greedy mode and non-greedy mode
For {n,m} This match way, in the end is match N or match m? This involves the problem of matching patterns. By default, quantifiers are as many matching characters as possible, known as greedy patterns, such as:
var num = ' 123456789 ';
Num.match (/\d{2,4}/g); [1234], [5678], [9]
and greedy mode for the non-greedy mode, only to be added after the quantifier "?" You can, for example, {n,m}?, which is matched by the fewest characters, as follows:
var num = ' 123456789 ';
Num.match (/\d{2,4}?/g); [12], [34], [56], [78], [9]
Group
Quantifiers can only match a single character multiple times if we want to match a set of characters multiple times? Regular expression parentheses can define a string as a whole as a grouping.
We want to match Apple this word appears 4 times this can match (apple) {4}, as follows:
If you want to match Apple or Orange 4 times, you can insert the pipe character "|", for example:
(Apple|orange) {4}
If multiple parentheses are present in a regular expression that is grouped, the matching result is grouped and numbered, for example:
(apple) \d+ (orange)
If we don't want to capture some groupings, just follow a question mark and a colon in front of the grouped parentheses, for example:
(?: Apple) \d+ (orange)
Boundary
Regular expressions also provide us with several common boundary-matching characters, such as:
Character |
Meaning |
^ |
Start with XX |
$ |
End With XX |
\b |
A word boundary, a character other than [a-za-z_0-9] |
\b |
Non-word boundaries |
Where the word boundary matches a position, one side of which is the character that makes the word, but the other side is a non word character, the beginning or end position of the string.
Prospect
Look forward to match the next occurrence is or not to a particular character set.
An expression |
Meaning |
EXP1 (? =exp2) |
The match is Exp2 's exp1. |
EXP1 (?!) EXP2) |
Matching the exp1 behind the EXP2. |
Look at an example:
Apple (? =orange)
(/apple (=orange)/). Test (' appleorange123 '); True
(/apple (? =orange)/). Test (' applepear345 ');//false
Let's look at another example:
Apple (?! Orange
(/apple (?!) Orange)/). Test (' appleorange123 '); False
(/apple (?!) Orange)/). Test (' applepear345 '); True
The above cliché JavaScript regular expression grammar is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.