JavaScript Core Reference Tutorials RegExp Objects _javascript Tips

Source: Internet
Author: User
Tags character classes instance method
  1. The direct measure character of a regular expression
    Character matching
    Alpha-numeric characters themselves
    \o nul character (\u0000)
    T-place table (\u0009)
    \ n line feed (\u000a)
    \v Vertical Position table (\u000b)
    \f Page Feed (\u000c)
    \ r Carriage return (\u000d)
    \xnn Latin characters specified by the hexadecimal number NN, such as \u0a equivalent to \ n
    \uxxxx Unicode characters specified by the hexadecimal number xxxx, such as \u0009 equivalent to \ t
    \CX control character X, for example, \CJ equivalent to \ n
  2. Character classes for regular expressions
    Character matching
    [...] Any character that is enclosed in parentheses
    [^...] Any character not in parentheses
    . Any character except newline and other Unicode line terminators
    \w any ASCII word character, equivalent to [a-za-z0-9_]
    \w any ASCII non-word character, equivalent to [^a-za-z0-9_]
    \s any Unicode whitespace characters [\f\n\r\t\v]
    \s any Unicode non-white-space character, note the difference between \w and \s [^\f\n\r\t\v]
    \d any ASCII number, equivalent to [0-9]
    \d any character other than the ASCII number, which is equivalent to [^0-9]
    [\b] backspace direct amount (special case)
  3. Repeating characters for regular expressions
    Character meaning
    {N,m} matches the previous item at least n times, but not more than m times
    {N,} matches the previous n times, or more times
    {n} matches the previous item exactly n times
    ? Matches the previous 0 or 1 times, which means that this item is optional. Equivalent to {0,1}
    + Match the previous item 1 or more times. Equivalent to {1,}
    * Match the previous item 0 or more times. Equivalent to {0,}
    In regular expressions, parentheses have several functions. One effect is to synthesize the subexpression into a separate project group so that it can be treated like a
    Separate units that use |, *, + or? Wait to deal with them. Another function of parentheses is to define the child mode in the complete pattern. When a
    When a regular expression successfully matches a target string, the part that matches the child pattern in parentheses can be extracted from the target string.
  4. Selection, grouping, and reference characters for regular expressions
    Character meaning
    | Choose. That matches the left subexpression of the symbol or the right subexpression
    (...) Combination. Combine several items into one unit, which is available by |, *, +, or? and other symbols used,
    You can also remember the characters that match this combination to use for subsequent references
    (?:...) Combined only. Combine items into one unit, but do not remember the characters that match the group
    \ n matches the first character of the Nth group, the group is a subexpression in parentheses (may be nested
    ). The group number is a left to right count of the number of open parentheses, and the group (?:) is not encoded in groups.
  5. anchor element of regular expression
    Character meaning
    ^ matches the beginning of a string, in multiple-row retrieval, matching the beginning of a line
    $ matches the end of a string, in multiple-row retrieval, matching the end of a line
    \b Matches the bounds of a word. In short, it is the position between the character \w and \w, or the character \w
    And the position of the beginning and end of the string (but note: [\b] matches the backspace)
    \b Characters that match non-word boundaries
    (? =p) forward declaration, which requires that the next character match the pattern p, but does not include those characters in the match
    (?! P) Reverse forward declaration, requiring that the next character not match the pattern p
  6. Flags for regular expressions
    Character meaning
    I perform case insensitive matching
    G performs a global match, in short, finds all matches, rather than finding the first one and then stopping
    M multiline mode, ^ matches the beginning of a line and the beginning of a string, $ matches the end of a line, and the end of a string

the RegExp object in JS

  1. constructor function:

    Explicit constructors, Syntax: New REGEXP ("pattern" [, "Flags"]).
    Implicit constructor, Syntax:/pattern/[flags].

  2. Static properties

    Index is the starting position of the first match for the current expression pattern, counting starting at 0. The initial value is-1, and the Index property will change each time a successful match is made. The
    input Returns the currently acting string, which can be abbreviated to $_ and the initial value to an empty string "".
    lastindex is the next position in the last character in the first match of the current expression pattern, counting from 0, and is often used as the starting position for continuing the search, with an initial value of-1, which means that the search starts at the start, and each successful When matched, the value of the Lastindex property changes. The
    lastmatch is the last matching string for the current expression pattern, which can be abbreviated to $&. Its initial value is an empty string "". The value of the Lastmatch property changes each time a successful match is made.
    lastparen If there are enclosed child matches in the expression pattern, it is the substring that the last child match in the current expression pattern matches to, and can be abbreviated to $+. Its initial value is an empty string "". Each time a successful match occurs, the value of the Lastparen property changes.
    leftcontext is all that is left of the last matching string in the current expression pattern, which can be abbreviated to $ ' (where "" is the inverted single quotation mark below "ESC" on the keyboard). The initial value is an empty string "". Each time a successful match is made, its property values change.
    $1...$9 These properties are read-only. If there is a $1...$9 in the expression pattern, the value of the property is captured by the 1th to 9th child match. If there are more than 9 child matches, the $1...$9 property corresponds to the last 9 substring respectively. In an expression pattern, you can specify any number of child matches with parentheses, but the RegExp object can only store the result of the last 9 child matches. In an array of results returned by some methods of the RegExp instance object, the results of the child matches within all parentheses are obtained.
  3. Instance Properties

    Global return The state of the global flag (g) specified when the RegExp object instance is created. If the G flag is set when the RegExp object instance is created, this property returns True, otherwise it returns false, and the default value is False. The
    ignoreCase Returns the status of the IgnoreCase flag (i) specified when the RegExp object instance was created. If the I flag is set when the RegExp object instance is created, this property returns True, otherwise it returns false, and the default value is False. The
    multiLine Returns the state of the MultiLine flag (m) specified when the RegExp object instance was created. If the M flag is set when the RegExp object instance is created, this property returns True, otherwise it returns false, and the default value is False.
    source Returns the expression text string specified when the RegExp object instance was created.
  4. Instance method

    Exec The syntax format is exec (str). This method searches for a string using the expression pattern specified when the RegExp object instance is created, and returns an array that contains the results of the search.
    If you set the global flag (g) for a regular expression, you can search the string continuously by calling the Exec and test methods multiple times, each time searching for a string from the location specified by the Lastindex property value of the RegExp object.
    If the global flag (g) is not set, the Exec and test methods ignore the Lastindex property value of the RegExp object and start the search from the beginning of the string. If the Exec method does not find a match, the return value is null, and if a match is found, an array is returned and the static property in the RegExp object is updated to reflect the match. The element 0 in the return array contains the full matching result, and the element 1~n, in turn, is the result of each child match defined in the expression pattern.
    Test The syntax format is Test (str). This method checks whether a string has an expression pattern specified when the instance of the RegExp object is created, returns True if it exists, or returns false. If a match is found, the static properties in the RegExp object are updated to reflect the match.
    Compile The syntax format is compile ("pattern" [, "Flags"]). The method can replace the expression pattern used by the RegExp object instance and compile the new expression pattern into an internal format so that subsequent matching procedures perform faster.
  5. RegExp description

    By default, regular expressions use the longest (also called greedy) matching principle. When? After the other qualifiers (*, + 、?、 {n}, {n,}, {n,m}) are followed, the matching pattern becomes the shortest (also called non greedy) matching principle.

    A grouping combination is a symbol that combines a part of a regular expression, and a reverse reference is used to match the content identifier captured by the previous grouping combination
    (1) (pattern) combines the pattern parts in parentheses into a combination of items and child matches that can be uniformly manipulated, each capturing
    are stored in the buffer in the order in which they appear from left to right in the regular expression pattern. The buffer is numbered from 1 and can store up to 99 child matching captures. The contents of the child matching capture stored in the buffer can be retrieved in the programming language. It can also be referenced in a regular expression. To match the literal parenthesis character "(and"), use "\" and "\" separately in the regular expression.
    (2) \num matches the contents of a buffer that is numbered num, where NUM is a single or two-bit decimal positive integer that identifies a particular buffer, which is called a reverse reference to a child match. One of the most useful applications of reverse references is the ability to provide the same matches, for example, To match a continuous 5 digit character, you can use \d{5} as the regular expression text, which can match 12345, however, to match consecutive 5 identical numeric characters, such as 55555, 11111, and so on, you need to use (\d) \1{4} as regular expression text, \1 representation with front (\d) As captured, \1{4} indicates that the contents of the previous (\d) capture also occur 4 consecutive times. For example, to match the cost of the ' is ' of gasoline going up? You can use/\b ([a-z]+) \1\b/gi as the regular expression text for all consecutive repeating parts of the word.
    (3) (?:p Attern) combines the pattern parts of parentheses into a single, unified operation, but does not capture this part as a child match, that is, the pattern part is a non capture match and its matching content is not stored in the buffer for later use. This pair must be combined, However, it is useful to not have a subset of the combination having a child matching feature.
    (4) (? =pattern) is referred to as forward "lookahead" matching, where there must be a pattern-matching content in the corresponding position of the searched string, but this part of the match is not processed as a matching result, and is not stored in the capture buffer for later use. (? =pattern) must be at the top or last side of a regular expression pattern.
    (5) (?! pattern) is called a reverse "lookahead" match, and the corresponding position of the searched string cannot have the pattern part matching content, in addition to the function as the forward "lookahead" match.


Example Demo
* Simple Example
<script type= "Text/javascript" ><!--function Demo () {var str = "ABCDD abcsszabcdddccababcddabcee"; var regex =/A (BC)/gi; var t = null; while (t = regex.exec (str)) {var = "index =" + T.index + ", match =" + t[0] + ", group =" + T[1 "; result = = "\n$1=" + regexp.$1 + ", lastmatch=" + Regexp.lastmatch + ", leftcontext=" + regexp.leftcontext; alert (result); }//--></script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

# Attention MATTERS
(? <=exp) 0 width is reviewed after the assertion (not supported)
(? <!exp) 0 width Negative review post assertion (not supported)
# Reference
Regular expression 30-minute introductory tutorial http://www.jb51.net/tools/zhengze.html
Related Article

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.