Selection of regular Expressions:
The choice is to select one of the many conditions, such as the sale of fruit of the big ye said to send you a fruit, you can choose "pear", "apple", "watermelon" in any one. Of course you will choose Watermelon, hehe.
Character "|" Used to separate alternative characters, such as/watermelon | pear | Apple/Match is "watermelon", "pear", or "apple". Similarly, if I want to match some rules based on the character class I talked about yesterday, I want to match 3 digits, or 4 lowercase letters, I can use the pattern/d{3}| [A-z] {4}/, hehe, how? That is to know the choice, and use the character class, at the same time we also used to repeat, these days to talk about things have reviewed, haha. The
selection is considered from left to right until a match is found. If the selection on the left matches, the right match is ignored, even if it produces a better match.
In fact, the above words is very good understanding, or take your uncle to send you fruit example, the first time to take out watermelon, ask you want? If you choose Watermelon, ye will not ask whether you want an apple or a pear. And if you don't want watermelon, then the big ye will take out a pear again, ask you to want? That's the truth.
Grouping of regular expressions:
We can group regular expressions by using parentheses. The so-called grouping, in fact, can be understood as a partner, and there is no commercial interests, the relationship between partners is a proud, a loss. That is to say, the things in parentheses as a group, a unit. Grouping is the subexpression in the regular expression, as we'll refer to in the following reference.
A unit is a description of the following repetition rules, and the "or" relationships between the groups are in groups. The things in the group are handled uniformly.
//The following mode can match Java, or it can match JavaScript
//Because? represents 0 or 1 times, if not, then the match is Java
The code is as follows |
Copy Code |
Otherwise, the match is JavaScript. var pattern =/java (script)?/; The following code returns the Abcdcd,cd,ef,,efnull var patt1=new RegExp (AB|CD) +| ( EF) "," G "); Todo { Result=patt1.exec ("Abcdcdef"); document.write (result); } while (Result!=null) |
In JavaScript1.5, you can combine items in a regular expression without having to create a reference with encodings. Instead of grouping items in parentheses, they are grouped by "(?:" and ")", but this grouping does not generate references (this is the reference below).
Reference to Regular Expressions:
In a regular expression, another function of the parentheses is to define the child mode in the complete pattern. When a regular expression succeeds in matching the target string, the part that matches the child pattern in parentheses can be extracted from the target string.
For example: I want to retrieve the condition is 3 letters plus 4 digits, but what really works for me is the 4 numbers. You can use the following pattern to match
3-Letter plus 4-digit matching pattern
The code is as follows |
Copy Code |
var pattern =/[a-z]{3}d{4}/; |
But how can I choose the number part of the string that matches my pattern? This is a problem. (Well, here's how to find out if the string that matches this pattern is not part of this log.) This is the first place to continue digging a hole. Well. At this point we need to turn that number part into a group, since it is grouped, then we can give the group a number. Oh.
3-Letter plus 4-digit matching pattern
The code is as follows |
Copy Code |
var pattern =/[a-z]{3} (d{4})/ |
Another use of parenthesized subexpression is to allow us to refer to the preceding subexpression in the back of the same regular expression (which leads to the point). This is accomplished by adding one or more digits after the character "". For example, a 1 reference refers to the first parenthesized subexpression, and 3 indicates that the third parenthesized subexpression is referenced.
Because the subexpression can be nested within another subexpression, its position is the position of the left parenthesis being counted.
The following example ([Ss]cript) is specified as 2
The code is as follows |
Copy Code |
var pattern =/([Jj]ava ([Ss]cript)?) Siss (funw*)/ |
The reference to the previous subexpression in the regular expression is not the pattern of that subexpression, but the text that matches that pattern, which is definitely not to be confused. Because you think that what you really care about is not the pattern, but the pattern is just a tool for finding what you want.
According to the above rules, I can empirically constrain that the individual parts of a string contain exactly the same characters. For example, my following regular expression matches 0 or more characters in single or double quotes. If I want the quotes to be matched (both single quotes or double quotes), you can use a reference.
The following code outputs True if the quotation marks are the same
code is as follows |
copy code |
var pattern = /["'][^"']*["']/; document.write (pattern. Test ("The" Best "things", are free)); //guarantees the same quotes as the following code output True,false var pattern =/(["']) [^" ']*1/; document.write (Pattern.test ("The ' best ' things in life are Free")); document.write (Pattern.test ("The Best" things in life are free)); |
Select, group, and reference operators for regular expressions:
Character Meanings
| Select. Matches the subexpression on the left of the symbol or the right subexpression, which is a Boolean or
(...) combination. Combine several items into one unit, which can be used by symbols such as *, +,?, and | And also remember the characters that match this combination for subsequent references using the
(?: ...) JavaScript 1.5 can be used to group regular expressions, but only in groups. Combines items into a single unit, but does not remember matching characters
N and nth groups that match the first time. A group is a subexpression in parentheses (nested, starting at the beginning of the left bracket, counting from 1, and counting) to (?: ...) Grouped groups are not encoded