js--the regular explanation

Source: Internet
Author: User
Tags control characters modifiers

1. Regular match rule literal string match metacharacters: Dot character (.) matches all characters except carriage return (\ r), newline (\ n), line delimiter (\u2028), and Segment Delimiter (\u2029).
  
The position character (^/$) ^/$ represents the start/end of the string in the position selector (|) representation or relationship, that is, cat|dog means matching cat or dog. The selector will include multiple characters before and after it, such as/ab|cd/refers to match ab or CD, not to match B or C. If you want to modify this behavior, you can use parentheses, the escape character regular the characters that have special meanings in the expression, and if you want to match them, you need to precede them with backslashes. For example, to match the plus sign, write +.
  
In regular mode, you need to escape with a slash, a total of 12 characters: ^ 、.、 [, $, (,), |, *, + 、?、 {and \. It is important to note that if you use the RegExp method to generate a regular object, the escape requires two slashes, because the inner string is escaped first.
  
The special character \cx represents Ctrl-[x], where X is any of the English letters in a-Z to match the control characters.
  
Match backspace (u+0008), do not confuse with \b.
  
\ n matches the line break key.
  
\ r matches the ENTER key.
  
\ t matches tab tab (U+0009).
  
\v matches the Vertical tab (U+000B).
  
\f matches a page break (u+000c).
  
To match the null character (u+0000).
  
Matches a character represented by a two-bit hexadecimal number (\X00-\XFF).
  
Matches a Unicode character represented by a four-bit hexadecimal number (\U0000-\UFFFF).
  
The character class (class) indicates that there are a range of characters to choose from, as long as you match one of them. All the available characters are placed in square brackets, such as [XYZ] for any of the selected matches in X, Y, and Z.
  
(1). Caret (^)
  
If the first character in the square brackets is [^], the characters in addition to the character class can be matched. For example, [^xyz] means that all but x, Y, and z can be matched.
  
If there are no other characters in the square brackets, that is, only [^], all characters are matched, including line breaks, and the dot (.) is not included in the newline character (2). hyphen (-)
  
In some cases, for consecutive sequences of characters, the hyphen (-) is used to provide a shorthand form that represents the continuous range of characters. For example, [ABC] can be written [a-c],[0123456789] can be written as [0-9], the same [A-z] represents 26 capital letters. Note: [1-31], which does not represent 1 to 31, represents only 1 to 3.
  
The predefined pattern \d matches any number between 0-9, which is equivalent to [0-9].
  
\d matches all characters other than 0-9, which is equivalent to [^0-9].
  
\w matches any letter, number, and underscore, which is equivalent to [a-za-z0-9_].
  
\w, except for all letters, numbers, and underscores, is equivalent to [^a-za-z0-9_].
  
\s matches spaces (including tab characters, space characters, break breaks, and so on), equal to [\t\r\n\v\f].
  
\s matches a character that is not a space, equivalent to [^\t\r\n\v\f].
  
\b matches the boundary of a word.
  
\b Matches a non-word boundary, that is, inside a word.
  
The exact number of occurrences of the repeating class pattern, expressed using braces ({}). {n} means to repeat n times, {n,} to repeat at least n times, {n,m} to repeat not less than n times, not more than m times.
  
Quantifier class? A question mark indicates that a pattern occurs 0 or 1 times, equivalent to {0, 1}.
  
* Asterisks indicate that a pattern appears 0 or more times, equivalent to {0,}.
  
The + plus sign indicates that a pattern appears 1 or more times, equivalent to {1,}.
  
Greedy mode is the maximum possible match by default, which is matched until the next character does not meet the matching rule. This is called greedy mode.
  
If you want to change the greedy mode to non-greedy mode, you can add a question mark after the quantifier character.
  
Greedy mode//non-greedy mode 123456123456*?: Indicates that a pattern occurs 0 or more times, and the match is non-greedy mode.
  
+?: Indicates that a pattern occurs 1 or more times, and that a non-greedy mode is used when matching.
  
The modifier modifier (modifier) represents an additional rule for the pattern, which is placed at the very end of the regular pattern.
  
Modifiers can be used either individually or in conjunction with multiple.
  
g modifier By default, when the first match succeeds, the regular object stops matching down. The G modifier represents global matching, and after that, the regular object matches all eligible results, primarily for search and replace.
  
The I modifier by default, the regular object distinguishes the case of letters, plus the I modifier indicates ignoring case (ignorecase).
  
The M modifier m modifier represents multiline mode (multiline), which modifies the behavior of ^ and $. By default (that is, without the M modifier), ^ and $ match the beginning and end of the string, plus the M modifier, and the ^ and $ will also match the beginning and end of the line, i.e. ^ and $ will recognize the newline character (\ n).
  
The parentheses of the group-matching regular expression represent a grouping match, and the patterns in parentheses can be used to match the grouped contents.
  
M123123 The above code, the regular expression/(.) B (.) /use two parentheses altogether, the first bracket captures a, and the second bracket captures c.
  
Note: When using group matching, it is not appropriate to use the G modifier at the same time, otherwise the match method does not capture the grouped content.
  
M123123 inside a regular expression, you can use \ n to refer to the bracketed content, n is the natural number starting at 1, which represents the parentheses in the corresponding order.
  
1212 in the first line of code above, \1 represents the previous parenthesis matching the content (that is, "a"), \2 represents the second parenthesis matching the content (that is, "B").
  
Parentheses can also be nested, at which point the \1 points to the outer brackets and \2 to the inner brackets.
  
1212 Non-capturing group: called the Non-capturing Group, which means that no match is returned for that group, that is, the parentheses are not counted in the matching results.
  
1212 the pattern in the above code, a total of two parentheses are used. The first parenthesis is a non-capturing group, so the last returned result does not have the first parenthesis, only the second bracket matches the content.
  
Antecedent assertion: called the antecedent assertion (Positive Look-ahead), x matches only before y, and y is not counted in the return result. For example, to match the number followed by a percent semicolon, you can write/\d+ (? =%)/.
  
1212 Antecedent negation assertion: called the antecedent negation assertion (negative look-ahead), X is not matched in front of y, and y is not counted as the return result. For example, to match a number that is not followed by a percent semicolon, write/\d+ (?!%) /。
  
12122. Regular expressions There are two ways to create a new regular expression.
  
One is to use a literal, with a slash to indicate the start and end.
  
The other is to use the RegExp constructor.
  
1212 the above two formulations are equivalent and create a new regular expression object with XYZ content. The main difference is that the first method creates a new regular expression at compile time, and the second method creates a new regular expression at run time.
  
After a regular object is generated, there are two ways to use it: the Regular object method: The string as a parameter, such as Regex.test (string).
  
Method of a String object: Use a regular object as a parameter, such as a string www.hjd1956.com/. Match (regex).
  
The attribute properties of a regular object are two classes of modifiers, which return a Boolean value that indicates whether the corresponding modifier is set.
  
: Returns a Boolean value that indicates whether the I modifier is set and the property is read-only.
  
: Returns a Boolean value that indicates whether the G modifier is set and the property is read-only.
  
: Returns a Boolean value that indicates whether the M modifier is set and the property is read-only.
  
A class of properties unrelated to modifiers, mainly the following two.
  
: Returns the location of the next search start. This property is read-write, but only meaningful when the G modifier is set.
  
: Returns the string form of the regular expression (excluding backslashes), which is read-only.
  
Method of the regular object (1) The test method of the test regular object returns a Boolean value that indicates whether the current pattern matches the argument string.
  
11 If the regular expression has a G modifier, each time the test method is matched backwards from where it last ended, you can also specify where to start the search by using the Lastindex property of the regular object.
  
property is valid only for the same regular expression 1212 the above code causes an infinite loop because each match condition of the while loop is a new regular expression, causing the Lastindex property to always be equal to 0.
  
If the regular pattern is an empty string, all strings are matched.
  
123123 the Exec method of the regular object, which can return the matching result. If a match is found, an array is returned, and the member is the substring of each successful match, otherwise NULL is returned.
  
123456123456 If the regular expression contains parentheses (that is, it contains a "group match"), the returned array will include more than one member. The first member is the result of a successful match, followed by a matching successful group of parentheses. In other words, the second member corresponds to the first parenthesis, the third member corresponds to the second parenthesis, and so on. The length property of the entire array equals the number of group matches plus 1.
  
The return array of the Exec method also contains the following two properties:: Entire original string.
  
: The entire pattern matches the starting position of the successful start (counting from 0).
  
If the regular expression is prefixed with the G modifier, you can use the Exec method multiple times and the next search position starts at the location where the last match was successfully completed.
  
123456789101112131415161718192021123456789101112131415161718192021 when the third match is finished, the entire string has reached the tail, the Lastindex property of the regular object is reset to 0, means that the fourth match will start from the beginning.
  
Using the G modifier to allow multiple matches, you can use a loop to complete the match.
  
}//b1234567891012345678910 If the regular object is an empty string, the Exec method matches successfully, but the return is also an empty string.
  
3. Method of the String object: Returns an array whose members are all matching substrings.
  
: Searches by a given regular expression, returns an integer that indicates where the match started.
  
: replaces the given regular expression by returning the replaced string.
  
: String segmentation by a given rule, returning an array containing the individual members after the split.
  
The match method of the string is very similar to the Exec method of the regular object: The match returns an array successfully, and the match fails to return NULL.
  
If the regular expression has a G modifier, the method behaves differently from the E-www.xbylpt5.cn xec method of the regular object, returning all successful results at once.
  
Sets the Lastindex property of the regular expression, which is invalid for the match method, and always begins with the first character of the string.
  
The search method of the string object, which returns the position of the first matching result in the entire string that satisfies the condition. If there is no match, 1 is returned.
  
After the regular expression uses the G modifier, use the Lastindex property to specify where to start the match, whether the result is invalid, or start with the first character of the string.
  
The Replace method of a string object can replace a matching value. It takes two parameters, the first one is the search pattern, and the second is the content of the substitution.
  
Search mode if you do not add the G modifier, replace the first matching successful value, or replace all successful matches.
  
One application of the method is to eliminate whitespace at both ends of the string.
  
The second argument of the 123123www.wmyld11.cn method can use the dollar sign $, which is used to refer to the substituted content.
  
$& refers to the matched substring.
  
$ ' refers to the text preceding the match result.
  
$ ' refers to the text that follows the match result.
  
$n refers to the nth set of content that matches successfully, N is the natural number starting at 1.
  
$$ refers to the dollar sign $.
  
The second argument of a method can also be a function that replaces each match with a function return value.
  
As a substitution function for the second parameter of the Replace method, you can accept multiple arguments. The first parameter is the captured content, and the second parameter is the group match that is snapped (how many groups match, and how many corresponding parameters). In addition, you can finally add two parameters, the second-to-last parameter is the position of the captured content in the entire string (for example, starting from the fifth position), and the last parameter is the original string.
  
The split method of the string object splits the string according to regular rules, returning an array of parts after the split.
  
The method accepts two parameters, the first parameter is a delimited rule, and the second parameter is the maximum number of members to return the array.

js--the regular explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.