In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "the usage of js regular expression grammar". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
This article introduces js regular expressions, as follows:
1. Regular expression rule
1.1 ordinary characters
Letters, numbers, Chinese characters, underscores, and punctuation marks that are not specifically defined in later chapters are all "ordinary characters". An ordinary character in an expression that matches the same character when it matches a string.
Example 1: the expression "c", when matching the string "abcde", the matching result is: successful; the matching content is: "c"; the matching position is: starts at 2 and ends at 3. (note: the subscript starts at 0 or 1, which may vary depending on the current programming language.)
Example 2: the expression "bcd", when matching the string "abcde", the matching result is: successful; the matching content is: "bcd"; the matching position is: starts at 1 and ends at 4.
1.2 simple escape characters
For some characters that are inconvenient to write, add "/" in front of them. In fact, we are already familiar with these characters.
Expression.
Can match
/ r, / n
Represents carriage return and newline characters
/ t
Tab character
/ /
Represent "/" itself
There are other punctuation marks that have special uses in later chapters, and the addition of "/" in front represents the symbol itself. For example: ^, $have a special meaning, if you want to match the "^" and "$" characters in the string, the expression needs to be written as "/ ^" and "/ $".
Expression.
Can match
/ ^
Match the ^ symbol itself
/ $
Match the $symbol itself
/.
Matches the decimal point itself.
The matching method of these escape characters is similar to that of "normal characters". It also matches the same character.
Example 1: the expression "/ $d", when matching the string "abc$de", the matching result is: successful; the matching content is: "$d"; the matching position is: starts at 3 and ends at 5.
1.3 expressions that can match 'multiple characters'
Some representations in regular expressions can match any one of the 'multiple characters'. For example, the expression "/ d" can match any number. Although you can match any of these characters, it can only be one, not multiple. It's like playing poker, big and small kings can replace any card, but only one card.
Expression.
Can match
/ d
Any number, any one of the 09s
/ w
Any letter or number or underscore, that is, any letter or number, that is, any letter or number or underscore, that is, any letter or number or underscore.
/ s
Any of the white space characters, including spaces, tabs, page feeds, etc.
.
The decimal point can match any character except the newline character (/ n)
Example 1: the expression "/ abc123 d". When matching "dram d", the result of the match is: successful; the content of the match is: "12"; the position of the match is: starts at 3 and ends at 5.
Example 2: the expression "A. aaa100 d". When matching "aaa100", the result of the match is: successful; the content of the match is "aa1"; the position of the match is: it starts at 1 and ends at 4.
1.4 Custom expressions that can match 'multiple characters'
Use square brackets [] to contain a series of characters that can match any one of them. If you include a series of characters with [^], you can match any character other than one of them. By the same token, although you can match any one of them, it can only be one, not more than one.
Expression.
Can match
[ab5@]
Match "a" or "b" or "5" or "@"
[^ abc]
Match any character other than "a", "b", "c"
[fmurk]
Match any letter between "f" and "k"
[^ A-F0-3]
Match any character other than "A" ~ "F", "0" ~ "3"
Example 1: when the expression "[bcd] [bcd]" matches "abc123", the result of the match is: successful; the content of the match is "bc"; the position of the match is: it starts at 1 and ends at 3.
Example 2: when the expression "[^ abc]" matches "abc123", the result of the match is: successful; the content of the match is: "1"; the position of the match is: starts at 3 and ends at 4.
1.5 A special symbol that modifies the number of matches
The expressions mentioned in the previous section can only match once, whether they can match only one character or any one of multiple characters. If you use an expression plus a special symbol that modifies the number of matches, you can repeat the match without having to rewrite the expression.
The way to use it is to place "degree modifier" after "modified expression". For example, "[bcd] [bcd]" can be written as "[bcd] {2}".
Expression.
Action
{n}
The expression is repeated n times, for example: "/ w {2}" is equivalent to "/ w aaaaa"; "a {5}" is equivalent to "WBO"
{m,n}
The expression should be repeated at least m times and n times at most. For example, "ba {1 baa 3}" can match "ba" or "baa" or "baaa".
{m,}
The expression is repeated at least m times, for example: "/ wgamble d {2,}" can match "A12", "_ 456", "M12344".
?
Match the expression 0 or 1 times, which is equivalent to {0jin1}, for example: "a [cd]?" Can match "a", "ac", "ad"
+
The expression appears at least once, which is equivalent to {1,}. For example, "afigb" can match "ab", "aab", "aaab".
*
The expression does not appear or appears any number of times, which is equivalent to {0,}, for example: "/ ^ * b" can match "b", "^ ^ b".
For example 1: when the expression "/ dharmpacer .roompacer d*" matches "It costs $12.5", the result is successful; the matching content is "12.5"; the position of the match is: starts at 10 and ends at 14.
Example 2: when the expression "Go {2 Go 8} gle" matches "Ads by goooooogle", the result of the match is: successful; the content of the match is "goooooogle"; the position of the match is: starts at 7 and ends at 17.
1.6 other special symbols that represent abstract meaning
Some symbols represent the special meaning of abstraction in expressions:
Expression.
Action
^
Matches the beginning of the string and does not match any characters
$
Matches the end of the string, not any characters
/ b
Matches a word boundary, that is, the position between a word and a space, and does not match any characters
Further textual explanations are still quite abstract, so give examples to help you understand.
Example 1: when the expression "^ aaa" matches "xxx aaa xxx", the result is: failed. Because "^" requires a match with the beginning of the string, "^ aaa" can match only if "aaa" is at the beginning of the string, such as "aaa xxx xxx".
Example 2: when the expression "aaa$" matches "xxx aaa xxx", the matching result is: failed. Because "$" requires a match with the end of the string, "aaa$" can match only if "aaa" is at the end of the string, such as "xxx xxx aaa".
Example 3: the expression ". / b." When matching "@ abc", the match result is: successful; the match content is "@ a"; the match position is: starts at 2 and ends at 4.
Further explanation: "/ b", similar to "^" and "$", does not match any characters, but it requires it to be on the left and right sides of the matching result, with a "/ w" range on one side and a non-"/ w" range on the other.
Example 4: when the expression "/ bend/b" matches "weekend,endfor,end", the matching result is: successful; the matching content is: "end"; the matching position is: starts at 15 and ends at 18.
Some symbols can affect the relationship between subexpressions within an expression:
Expression.
Action
| |
The OR relationship between the left and right expressions, matching the left or right
(.)
(1)。 When the number of matches is modified, the expression in parentheses can be modified as a whole
(2)。 When taking the matching result, the content matched by the expression in parentheses can be obtained separately.
Example 5: when the expression "Tom | Jack" matches the string "Ichimm Tom, he is Jack", the matching result is: successful; the matching content is "Tom"; the matching position is: starts at 4 and ends at 7. When matching the next one, the match result is: successful; the match content is "Jack"; when the match is reached, it starts at 15 and ends at 19.
Example 6: the expression "(go/s*) +" matches "Let's go go go!" The match result is: successful; the match to the content is: "go go go"; the match position is: starts at 6 and ends at 14.
For example 7: the expression "¥(/ dhammer pacemaker)" when matching "$10.9, ¥20.5", the result of the match is: success; the content of the match is "¥20.5"; the position of the match is: starts at 6 and ends at 10. Get the parenthesis range separately to match: "20.5".
two。 Some advanced rules in regular expressions
2.1 greed and non-greed in the number of matches
When using a special symbol that modifies the number of matches, there are several representations that enable the same expression to match different times, such as "{mforce n}", "{m,}", "?", "*", "+", depending on the string being matched. This kind of expression with indefinite number of repeated matches always matches as many times as possible in the matching process. For example, for the text "dxxxdxxxd", an example is as follows:
Expression.
Matching result
(d) (/ w +)
"/ w +" will match all characters "xxxdxxxd" after the first "d"
(d) (/ w +) (d)
"/ w +" will match all characters "xxxdxxx" between the first "d" and the last "d". Although "/ w +" can also match the last "d", in order for the entire expression to match successfully, "/ w +" can "give up" the last "d" it could have matched.
Thus it can be seen that when matching, "/ w +" always matches as many characters as possible that conform to its rules. Although in the second example, it does not match the last "d", it is also for the entire expression to match successfully. By the same token, expressions with "*" and "{mrecom n}" match as many as possible, with "?" When the expression can be matched but not matched, it is also "to match" as much as possible. This matching principle is called the "greed" mode.
Non-greedy mode:
Add a "?" after modifying the special symbol of the number of matches. Number, you can make the expressions with variable matching times match as little as possible, and make the expressions that can match but not match "mismatch" as much as possible. This matching principle is called "non-greedy" mode, also known as "reluctant" mode. If the lack of matching will lead to the failure of the whole expression matching, similar to the greedy pattern, the non-greedy pattern will match a little more to make the whole expression match successfully. For example, for the text "dxxxdxxxd":
Expression.
Matching result
(d) (/ wrestling?)
"/ wicked?" The characters after the first "d" will be matched as little as possible, and the result will be "/ wicked?" Only one "x" is matched.
(d) (/ wicked?) (d)
In order for the entire expression to match successfully, "/ wicked?" You have to match "xxx" to match the following "d" so that the entire expression matches successfully. So, the result is "/ wicked?" Match "xxx"
For more cases, examples are as follows:
Example 1: expression "(. *)" and string "
Aa
Bb
"when matching, the result of the match is: successful; the content of the match is"
Aa
Bb
"the entire string, the"in the expression will match the last"in the string."
Example 2: by contrast, the expression "(. *?)" When matching the same string in example 1, you will only get "
Aa
"when you match the next one again, you can get the second one."
Bb
".
2.2 reverse reference / 1, / 2.
When an expression matches, the expression engine records the string matched by the expression contained in parentheses "()". When getting the matching result, the string matched by the expression contained in parentheses can be obtained separately. This has been demonstrated many times in the previous example. In practical applications, when a certain boundary is used to find it, and the content you want to obtain does not contain a boundary, you must use parentheses to specify the desired scope. For example, the front "(. *?)".
In fact, the string matched by the expression contained in parentheses can be used not only after the end of the match, but also during the matching process. The part after the expression can refer to the previous "the submatch in parentheses matches the string that has been matched". The reference method is "/" plus a number. "/ 1" refers to the string matched in the first pair of parentheses, "/ 2" refers to the string matched in the second pair of parentheses. And so on, if one pair of parentheses contains another pair of parentheses, the outer parentheses are numbered first. In other words, which pair of left parentheses "(" comes first, then the pair will be numbered first.
Examples are as follows:
Example 1: the expression "('|") (. *?) (/ 1) "when matching" 'Hello', "World", the matching result is: successful; the matching content is:"' Hello' ". When you match the next one, you can match" World ".
Example 2: expression "(/ w) / 1 {4,}" when matching "aa bbbb abcdefg ccccc 111121111 999999999", the matching result is: successful; the matching content is "ccccc". When you match the next one again, you will get 999999999. This expression requires characters in the "/ w" range to be repeated at least 5 times, paying attention to the difference with "/ w {5,}".
Example 3: expression "
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.