What is a regular expression?
A regex (regular expression) is a string containing a normal string and a special metacharacters (meta characters) or meta-sequences. Normal strings match themselves. Metacharacters and meta sequences are characters or sequences of characters that represent the number, position, or character type.
This is just a record of the learning process, please go to JS regular expression details
Common regular characters
1. \
: Mark the next character 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 line break. The sequence ' \ ' matches "and" (Then matches "(".
2. ^
: matches the starting position of the input string. If the Multiline property of the RegExp object is set, ^ also matches the position after ' n ' or ' R '.
3. $
: Matches the end position of the input string. If the Multiline property of the RegExp object is set, $ also matches the position before ' n ' or ' R '.
4. *
: Matches the preceding subexpression 0 or more times. For example, Zo can match "z" and "Zoo". equivalent to {0,}.
5. +
: Matches the preceding subexpression one or more times. For example, ' zo+ ' can match "Zo" and "Zoo", but not "Z". + equivalent to {1,}.
6. ?
: match the preceding subexpression 0 or one time. For example, "Do (es)?" can match "do" in "do" or "does".? Equivalent to {0,1}.
7. {n}
: N is a non-negative integer. Matches the determined n times. For example, ' o{2} ' cannot match ' o ' in ' Bob ', but can match two o in ' food '.
8. {n,}
: N is a non-negative integer. Match at least n times. For example, ' o{2,} ' cannot match ' o ' in ' Bob ', but can match all o in ' Foooood '. ' O{1,} ' is equivalent to ' o+ '. ' O{0,} ' is equivalent to ' o* '.
9. {n,m}
: M and n are non-negative integers, where n <= m. Matches at least n times and matches up to M times. For example, "o{1,3}" will match the first three o in "Fooooood". ' o{0,1} ' is equivalent to ' O? '. Note that there can be no spaces between a comma and two numbers.
?
: When the character immediately follows any other restriction (*, +,?, {n}, {n,}, {n,m}), the matching pattern is non-greedy. The non-greedy pattern matches the searched string as little as possible, while the default greedy pattern matches as many of the searched strings as possible. For example, for the string "oooo", ' o+? ' will match a single "O", while ' o+ ' will match all ' o '.
One. ·
: matches any single character except for "N". To match any character including ' n ', use a pattern like ' [. N] '.
(pattern)
: Match pattern and get this match. The obtained matches can be obtained from the resulting Matches collection, the Submatches collection is used in VBScript, and 0... is used in JScript. 0 ... 9 properties. To match the parentheses character, use ' (' or ') '.
(?:pattern)
: Matches pattern but does not get matching results, which means that this is a non-fetch match and is not stored for later use. This is useful when using the "or" character (|) to combine parts of a pattern. For example, ' Industr (?: y|ies) is a more abbreviated expression than ' industry|industries '.
N. (?=pattern)
: Forward-checking matches the lookup string at the beginning of any string that matches the pattern. This is a non-fetch match, which means that the match does not need to be acquired for later use. For example, ' Windows (? =95|98| nt|2000) ' Can match Windows 2000 ', but does not match Windows 3.1 in Windows. Pre-checking does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, rather than starting with the character that contains the pre-check.
The. (?!pattern)
: Negative pre-check to match the find string at the beginning of any mismatch between the pattern string. This is a non-fetch match, which means that the match does not need to be acquired for later use. For example ' Windows (?! 95|98| nt|2000) ' can match Windows 3.1 ', but does not match Windows 2000 in Windows. Pre-check does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, rather than starting with the character that contains the pre-check
x|y
x.: match x or Y. For example, ' Z|food ' can match "z" or "food". ' (z|f) Ood ' matches "Zood" or "food".
. [xyz]
: Character set fit. Matches any one of the characters contained. For example, ' [ABC] ' can match ' a ' in ' plain '.
. [^xyz]
: Negative character set. Matches any character that is not contained. For example, ' 1 ' can match ' P ' in ' plain '.
. [a-z]
: The range of characters. Matches any character within the specified range. For example, ' [A-z] ' can match any lowercase alphabetic character in the ' a ' to ' Z ' range.
. [^a-z]
: Negative character range. Matches any character that is not in the specified range. For example, ' 2 ' can match any character that is not within the range of ' a ' to ' Z '.
\b
: Matches a word boundary, that is, the position between the word and the space. For example, ' Erb ' can match ' er ' in ' never ', but not ' er ' in ' verb '.
\B
: Matches a non-word boundary. ' ErB ' can match ' er ' in ' verb ', but cannot match ' er ' in ' Never '.
\cx
: matches the control character indicated by X. For example, CM matches a control-m or carriage return character. The value of x must be one of a-Z or a-Z. Otherwise, c is treated as a literal ' C ' character.
. \d
: matches a numeric character. equivalent to [0-9].
\D
: Matches a non-numeric character. Equivalent to 3.
A. \f
: Matches a page break. Equivalent to x0c and CL.
. \n
: Matches a newline character. Equivalent to x0a and CJ.
A. \r
: matches a carriage return character. Equivalent to x0d and CM.
\s
: matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [FNRTV].
The. \S
: matches any non-whitespace character. Equivalent to 4.
. \t
: matches a tab character. Equivalent to x09 and CI.
A. \v
: Matches a vertical tab. Equivalent to x0b and CK.
\w
: matches any word character that includes an underscore. Equivalent to ' [a-za-z0-9_] '.
The. \W
: matches any non-word character. Equivalent to ' 5 '.
A. \xn
: Matches N, where n is the hexadecimal escape value. The hexadecimal escape value must be two digits long for a determination. For example, ' x41 ' matches ' A '. ' x041 ' is equivalent to ' x04 ' & ' 1 '. ASCII encoding can be used in regular expressions.
. \num
: Matches num, where num is a positive integer. A reference to the obtained match. For example, ' (.) 1 ' matches two consecutive identical characters.
Panax Notoginseng \n
.: Identifies an octal escape value or a backward reference. If n has at least n obtained sub-expressions, then n is a backward reference. Otherwise, if n is the octal number (0-7), N is an octal escape value.
Max. \nm
: Identifies an octal escape value or a backward reference. NM is a backward reference if at least NM has a sub-expression before nm. If there are at least n fetches before nm, then n is a backward reference followed by the literal m. If none of the preceding conditions are met, if both N and M are octal digits (0-7), NM will match the octal escape value nm.
Max. \nml
: If n is an octal number (0-3) and both M and L are octal digits (0-7), the octal escape value NML is matched.
JS Regular expression characters commonly used-character