js regular notes

Methods exec

If a non-global match

  • Call non-global RegExp object exec (time), it returns an array
  • The first element is the regular expression to match the text
  • The second element of the first subexpression matches RegExpObject text (if any)
  • The third element is a second object RegExpObject subexpression matches the text (if any), and so on
eg:var reg=/\d(\w)(\w)\d/;
var str='$1aa2bb3cc4dd5e'
var ret = reg.exec(str);

则ret返回数组为  ["1aa2","a","a"]

Character Description

  • \ The next character is marked as a special character, or a literal character, or a backward reference, or an octal escape. For example, 'n' matches the character "n". '\ N' matches a newline. Sequence '\' match "\" and "(" the match "(."
  • Match with input the word character string of the junction beam position counter . As if set set of R & ltEGEX P of the object of MU L T I L I n- E genus properties , Also matches the position before the '\ n' or '\ r'.
  • ^ Matches the beginning of the string. If the object is set RegExp Multiline property, also matches ^ '\ n' position after or '\ r'.
    • Matches the preceding subexpression zero or more times. For example, zo * matches "z" and "zoo". * Is equivalent to {0}.
    • Matches the preceding subexpression one or more times. For example, 'zo +' will match "zo" and "zoo", but can not match the "z". + Is equivalent to {1}.
  • ? Matches the preceding subexpression zero or one. For example, "do (es)?" Matches "do" or "does" in the "do". ? Is equivalent to {0,1}.
  • {N} n is a non-negative integer. Matching the determined n times. For example, 'o {2}' does not match the "Bob" in the 'o', but can match the "food" in the two o.
  • {N,} n is a non-negative integer. Matching at least n times. For example, 'o {2,}' does not match the "Bob" in the 'o', but it can match all o "foooood" in. 'O {1,}' is equivalent to 'o +'. 'O {0,}' is equivalent to 'o *'.
  • {N, m} m and n are non-negative integers, where n <= m. Match at least n times and match up to m times. For example, "o {1,3}" will match "fooooood" in the previous three o. 'O {0,1}' is equivalent to 'o?'. Please note that no spaces between the comma and the two numbers.
  • ? When the character immediately to any other qualifier (*, +,?, {N}, {n,}, {n, m}) when the rear, non-greedy matching pattern. Non-greedy pattern matches as little as possible the search string, and the default greedy pattern matches as much of the string search. For example, the string "oooo", 'o +?' Matches a single "o", and 'o +' will match all 'o'.
  • . Matches any single character except "\ n" is. To match include '\ n', including any character, like the use of '[. \ N]' mode.
  • (Pattern) matches the pattern and get the match. The matching can be obtained from the Matches have been used in VBScript SubMatches set is used in JScript 0 9 property. To match parentheses characters, use '(' or ')'.
  • (:? pattern) matches the pattern but do not get matching results, that this is a non-access match, not stored for later use. This use of "or" character (|) to combine the various parts of a model is useful. For example, 'industr (:? Y | ies) is a ratio of' industry | more brief expressions industries'.
    (? = pattern) Positive pre-investigation, matching the search string at the beginning of the string any pattern matching. This is a non-access match, that is, the match does not need to obtain for later use. For example, 'Windows (= 95 |? 98 | NT | 2000)' can match the "Windows 2000" in the "Windows", but can not match the "Windows 3.1" "Windows". Pre-check does not consume characters, that is, after a match occurs, the last match after the next match to start the search immediately, rather than starting from the characters that contains pre-investigation.
  • (?! Pattern) Negative pre-investigation, at the beginning of any match pattern string matching search string. This is a non-access match, that is, the match does not need to obtain for later use. For example 'Windows (95 |?! 98 | NT | 2000)' can match the "Windows 3.1" "Windows", but can not match
  • "Windows 2000" in the "Windows". Pre-check does not consume characters, that is, after a match occurs, the last match after the next match to start the search immediately, rather than starting from the characters that contains pre-investigation.
  • x | y Matches either x or y. For example, 'z | food' can match the "z" or "food". '(Z | f) ood' the match "zood" or "food".
  • [Xyz] set of characters. Matches any character included. For example, '[abc]' matches "plain" in the 'a'.
  • [^ Xyz] negative set of characters. Matches any character not included. For example, '[^ abc]' matches "plain" the 'p', 'l', 'i', 'n'.
  • [Az] character range. Matches any character within the specified range. For example, '[az]' match 'a' to any lowercase alphabetic characters 'z' within range.
  • [^ Az] Negative character range. Matches any character not in any specified range. For example, '[^ az]' can not match any 'a' to an arbitrary character 'z' within range.
  • \ B matches a word boundary, that is, refers to the location and spaces between words. For example, 'er \ b' matches "never" in the 'er', but does not match the "verb" in the 'er'.
  • \ B matches non-word boundary. 'Er \ B' matches "verb" in the 'er', but does not match the "never" in the 'er'.
  • \ Cx matches control characters specified by the x. For example, \ cM matches a Control-M or carriage return. The value of x must be AZ or az. Otherwise, c as a literal 'c' character.
  • \ D Matches a digit character. Equivalent to [0-9].
  • \ D Matches a non-numeric characters. It is equivalent to [^ 0-9].
  • \ F match for a website page. Equivalent to \ x0c and \ cL.
  • \ N Matches a newline. Equivalent to \ x0a and \ cJ.
  • \ R match a carriage return. Equivalent to \ x0d and \ cM.
  • \ S Matches any whitespace characters, including spaces, tabs, page breaks, and so on. It is equivalent to [\ f \ n \ r \ t \ v].
  • \ S Matches any non-whitespace characters. Is equivalent to [^ \ f \ n \ r \ t \ v].
  • \ T matches a tab. Equivalent to \ x09 and \ cI.
  • \ V matches a vertical tab. Equivalent to \ x0b and \ cK.
  • \ W matches any word character including underscore. It is equivalent to the '[A-Za-z0-9_]'.
  • \ W matches any non-word character. It is equivalent to the '[^ A-Za-z0-9_]'.
  • \ Xn Matches n, where n is a hexadecimal escape value. Hexadecimal escape values ​​must be determined by two digits long. For example, '\ x41' match "A". '\ X041' is equivalent to '\ x04' & "1". Regular expressions can be used in ASCII encoding.
  • \ Match num num, where num is a positive integer. A reference to the acquired match. For example, '(.) \ 1' matches two consecutive identical characters.
  • \ N identifies an octal escape value or a backward reference. If before \ n at least n captured subexpressions, n is a reference back. Otherwise, if n is an octal digit (0-7), then n is an octal escape value.
  • \ Nm identifies an octal escape value or a backward reference. If you have to obtain a sub-expressions nm at least before nm \, then nm is a reference back. If \ There are at least n before obtaining nm, then n is a backward reference character m in the heel. When these conditions are not met, if n and m are octal digits (0-7), \ nm matches octal escape value nm.
  • \ NML If n is an octal digit (0-3), and m and l are octal digits (0-7), the matching octal escape value nml.
  • \ Un Matches n, where n is a Unicode character with four hexadecimal digits represented. For example, \ u00A9 matching copyright symbol (?).
Released four original articles · won praise 0 · Views 1391

Guess you like

Origin blog.csdn.net/sumtre_w/article/details/53048214