Js 常用正则表达式
| Character | Meaning |
|---|---|
\ |
Matches according to the following rules: |
^ |
Matches beginning of input. If the multiline flag is set to true, also matches immediately after a line break character. For example, /^A/ does not match the 'A' in "an A", but does match the 'A' in "An E".The ' ^' has a different meaning when it appears as the first character in a character set pattern. See complemented character sets for details and an example. |
$ |
Matches end of input. If the multiline flag is set to true, also matches immediately before a line break character. For example, |
* |
Matches the preceding expression 0 or more times. Equivalent to {0,}. For example, |
+ |
Matches the preceding expression 1 or more times. Equivalent to For example, |
? |
Matches the preceding expression 0 or 1 time. Equivalent to {0,1}.For example, /e?le?/ matches the 'el' in "angel" and the 'le' in "angle" and also the 'l' in "oslo".If used immediately after any of the quantifiers *, +, ?, or {}, makes the quantifier non-greedy (matching the fewest possible characters), as opposed to the default, which is greedy (matching as many characters as possible). For example, applying /\d+/ to "123abc" matches "123". But applying /\d+?/ to that same string matches only the "1".Also used in lookahead assertions, as described in the x(?=y) and x(?!y) entries of this table. |
. |
(The decimal point) matches any single character except the newline character. For example, |
(x) |
Matches 'x' and remembers the match, as the following example shows. The parentheses are called capturing parentheses. |
(?:x) |
Matches 'x' but does not remember the match. The parentheses are called non-capturing parentheses, and let you define subexpressions for regular expression operators to work with. Consider the sample expression /(?:foo){1,2}/. If the expression was /foo{1,2}/, the {1,2} characters would apply only to the last 'o' in 'foo'. With the non-capturing parentheses, the {1,2} applies to the entire word 'foo'. For more information, see Using parentheses below. |
x(?=y) |
Matches 'x' only if 'x' is followed by 'y'. This is called a lookahead. For example, |
x(?!y) |
Matches 'x' only if 'x' is not followed by 'y'. This is called a negated lookahead. For example, |
x|y |
Matches 'x', or 'y' (if there is no match for 'x'). For example, |
{n} |
Matches exactly n occurrences of the preceding expression. N must be a positive integer. For example, /a{2}/ doesn't match the 'a' in "candy," but it does match all of the a's in "caandy," and the first two a's in "caaandy." |
{n,} |
Matches at least n occurrences of the preceding expression. N must be a positive integer. For example, /a{2,}/ will match "aa", "aaaa" and "aaaaa" but not "a" |
{n,m} |
Where For example, |
[xyz] |
Character set. This pattern type matches any one of the characters in the brackets, including escape sequences. Special characters like the dot(.) and asterisk (*) are not special inside a character set, so they don't need to be escaped. You can specify a range of characters by using a hyphen, as the following examples illustrate.The pattern [a-d], which performs the same match as [abcd], matches the 'b' in "brisket" and the 'c' in "city". The patterns /[a-z.]+/ and /[\w.]+/ match the entire string "test.i.ng". |
[^xyz] |
A negated or complemented character set. That is, it matches anything that is not enclosed in the brackets. You can specify a range of characters by using a hyphen. Everything that works in the normal character set also works here. For example, |
[\b] |
Matches a backspace (U+0008). You need to use square brackets if you want to match a literal backspace character. (Not to be confused with \b.) |
\b |
Matches a word boundary. A word boundary matches the position where a word character is not followed or preceded by another word-character. Note that a matched word boundary is not included in the match. In other words, the length of a matched word boundary is zero. (Not to be confused with Examples: Note: JavaScript's regular expression engine defines a specific set of charactersto be "word" characters. Any character not in that set is considered a word break. This set of characters is fairly limited: it consists solely of the Roman alphabet in both upper- and lower-case, decimal digits, and the underscore character. Accented characters, such as "é" or "ü" are, unfortunately, treated as word breaks. |
\B |
Matches a non-word boundary. This matches a position where the previous and next character are of the same type: Either both must be words, or both must be non-words. The beginning and end of a string are considered non-words. For example, |
\cX |
Where X is a character ranging from A to Z. Matches a control character in a string. For example, |
\d |
Matches a digit character. Equivalent to For example, |
\D |
Matches a non-digit character. Equivalent to For example, |
\f |
Matches a form feed (U+000C). |
\n |
Matches a line feed (U+000A). |
\r |
Matches a carriage return (U+000D). |
\s |
Matches a single white space character, including space, tab, form feed, line feed. Equivalent to For example, |
\S |
Matches a single character other than white space. Equivalent to For example, |
\t |
Matches a tab (U+0009). |
\v |
Matches a vertical tab (U+000B). |
\w |
Matches any alphanumeric character including the underscore. Equivalent to For example, |
\W |
Matches any non-word character. Equivalent to For example, |
\n |
Where n is a positive integer, a back reference to the last substring matching the n parenthetical in the regular expression (counting left parentheses). For example, |
\0 |
Matches a NULL (U+0000) character. Do not follow this with another digit, because \0<digits> is an octal escape sequence. Instead use \x00. |
\xhh |
Matches the character with the code hh (two hexadecimal digits) |
\uhhhh |
Matches the character with the code hhhh (four hexadecimal digits). |
\u{hhhh} |
(only when u flag is set) Matches the character with the Unicode value hhhh (hexadecimal digits). |
浙公网安备 33010602011771号