In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use RegExp regular expressions in javascript, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
What is a regular expression?
Regular expression, also known as "Regular Expression" (RegExp), is a concept in computer science.
What is the use of regular expressions?
It is often used to search and replace texts that match a certain pattern.
Regular expressions are the best choice for matching special characters or characters with special collocation principles.
Escape character "\"
Example: adding a "character" to var str = "asdfghj" is not valid under normal circumstances, but it can be established by using the escape character "\" in regular expressions.
Adding the escape character "\" before "can make the variable set. In the figure, the escape character + double quotation marks successfully becomes a text symbol, and then you can output" asdf "ghjs" in the browser. "
String newline character\ n
Example: line wrapping in var str = "asdfghj"
Get the result
How to create a regular expression
1. Direct quantity
Var reg = / /; write between slashes
Var reg = / abc/; means to match a regular abc, where var str = "abcdef". Test whether str contains the string specified by reg through reg.test (str). If true is returned, false is not returned.
Writable attribute values (I, m, g) after / /
2.new RegExp ()
The effect is the same as the direct quantity containing the value in the regular expression returns true, if it does not contain the return false
Writable attribute value in RegExp: RegExp ("abc", parameter (iMagnegPhonm))
Three modifiers for regular expressions: I, m, g
I: case-insensitive
M: perform multiline matching
Var reg = / ^ a str; indicates that the opening character of the search is a. There are no characters meeting this requirement in the str at this time, but the newline character\ n can be recognized if multi-line matching is performed. When there is a newline character, it is considered to be a line before and after\ n.
G: perform a global match (find all matches instead of stopping after the first match is found)
Method in regular expression: reg.test (); return result true/false
The method in the variable: str.match (); the result can be returned, the effect is more intuitive
Expression.
[]: can be written in [] range
[^ abc]: starts with abc
[0-9]: range 0-9
[a murz]: range a murz
[Amurz] range Amurz
[Amurz] range Amurz Zgami akoz
|: indicates or
Metacharacters:
\ W: word character
\ W: non-word character
\ d: number
\ d: non-numeric
\ s: blank characters (including: space character, tab character, carriage return character, line feed character, vertical line feed character, page feed character)
\ s: non-white space character
\ n: newline character
\ r: carriage return
\ b: word boundary
\ B: non-word boundary
\ t: tabs
. : represents all characters except\ r\ n
Quantifier (the following n is a word for quantity)
Nasty: can appear 1 to countless times
Nails: it can occur 0 to countless times, and the logical distance at the end is empty.
N? 0 or a string can appear, and the logical distance is empty
N {X}: X n strings can appear
N {XQuery Y}: match a string containing X to Y n (as much as possible in accordance with the greedy matching principle)
N {X,}: match a string containing at least X n (as many as possible in accordance with the principle of greedy matching)
^ n: start with n
Nails: ends with n
ReegExp object properties:
Whether the ignoreCase:RegExp object contains the modifier I
Global: whether the RegExp object contains the modifier g
Multiline: whether the RegExp object contains the modifier m
Source: displays the regular expression function body
Regular expression method:
Test: check the value specified in the string. Return to ture/false
Exec: check the value specified in the string. Returns the value and determines its location.
The position of the match changes with the starting position of its cursor. When the cursor moves to the last bit, it returns null, and if executed again, it will be executed from scratch.
In the following picture
"ab" did not return a value
Index is the cursor position
Method of string object:
Match: find a match for one or more regular expressions. Returns the matching value.
Search: checks the value that matches the regular expression and returns its cursor position. If the match does not return-1.
Split: splits the string.
Replace: replaces a string that matches a regular expression.
Example: replace var str = "aabb" with "bbaa"
1. Var reg = / (\ w)\ 1 (\ w)\ 2Unig
Console.log (str.replace (reg, "$2 $2 $1 $1"))
\\ output result is: "bbaa"
2. Var reg = / (\ w)\ 1 (\ w)\ 2Unig
Console.log (str.replace (reg, function ($, $1, $2) {
Return $2 + $2 + $1 + $1
}))
\\ output result is: "bbaa"
Where $is the global regular expression, $1 is the first argument "(\ w)\ 1", and $2 represents the second argument "(\ w)\ 2".
Example: the-first-name changes to small head peak mode (theFirstName)
Where $is the global regular expression, and $1 is the first argument "- (\ w)".
Forward pre-check (positive assertion): participate only in the rule and do not participate in the selection
1. Look at the string followed by b in var str = "abaaaa" in the regular expression, but the b character is not displayed in the output
Var str = "abaaaa"
Var reg = / a (? = b) / g; / / means an is followed by b, but b participates in the selection and only participates in the qualification.
two。 Look at the string without b followed by var str = "abaaaa" in the regular expression
Var reg = / a (?! B) / g; / / indicates a string with no b character after looking for a
Non-greedy matching: greedy matching is the default in regular expressions, but we can change greedy matching into non-greedy matching. Add after any quantifier?
Reg = / n {1,} /; / / then the number of n is between 1 and infinity, and under the action of greedy matching, the number of values will be as large as possible.
Reg = / n (1,)? / / add after n (1,)? In this case, the regular match will be based on the minimum number of values
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.
The matching result (d) (\ w +) "\ w +" will match all the characters "xxxdxxxd" (d) (\ w +) (d) "\ w +" after the first "d" will match all the 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.
Match result (d) (\ wicked?) "\ wicked?" The characters after the first "d" will be matched as little as possible, and the result will be: "\ wicked?" Only one "x" (d) (\ wicked?) (d) is matched. 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, and "\ 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.