Open ythy opened 6 years ago
Character Sets
Character | Meaning |
---|---|
[xyz] [a-c] |
A character set. Matches any one of the enclosed characters. You can specify a range of characters by using a hyphen, but if the hyphen appears as the first or last character enclosed in the square brackets it is taken as a literal hyphen to be included in the character set as a normal character. It is also possible to include a character class in a character set. For example, [abcd] is the same as [a-d] . They match the "b" in "brisket" and the "c" in "chop".For example, [abcd-] and [-abcd] match the "b" in "brisket", the "c" in "chop" and the "-" (hyphen) in "non-profit". For example, [\w-] is the same as [A-Za-z0-9_-] . They match the "b" in "brisket", the "c" in "chop" and the "n" in "non-profit". |
[^xyz] [^a-c] |
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, but if the hyphen appears as the first or last character enclosed in the square brackets it is taken as a literal hyphen to be included in the character set as a normal character. For example, [^abc] is the same as [^a-c] . They initially match "o" in "bacon" and "h" in "chop". |
x\|y |
Matches either x or y . For example, /green\|red/ matches "green" in "green apple" and "red" in "red apple". |
^ |
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 first "A" in "An A". |
$ |
Matches end of input. If the multiline flag is set to true, also matches immediately before a line break character. For example, /t$/ does not match the "t" in "eater", but does match it in "eat". |
\b |
Matches a word boundary. This is the position where a word character is not followed or preceded by another word-character, such as between a letter and a space. Note that a matched word boundary is not included in the match. In other words, the length of a matched word boundary is zero. Examples: /\bm/ matches the 'm' in "moon" ;//oo\b/ does not match the 'oo' in "moon", because 'oo' is followed by 'n' which is a word character;//oon\b/ matches the 'oon' in "moon", because 'oon' is the end of the string, thus not followed by a word character;//\w\b\w/ will never match anything, because a word character can never be followed by both a non-word and a word character.\b matches the empty string at the beginning or end of a word. |
\B |
Matches a non-word boundary. This is a position where the previous and next character are of the same type: Either both must be words, or both must be non-words. Such as between two letters or between two spaces. The beginning and end of a string are considered non-words. Same as the matched word boundary, the matched non-word bondary is also not included in the match. For example, /\Bon/ matches "on" in "at noon", and /ye\B/ matches "ye" in "possibly yesterday".\B matches the empty string not at the beginning or end of a word. |
(x) |
Matches x and remembers the match. These are called capturing groups. For example, /(foo)/ matches and remembers "foo" in "foo bar". The capturing groups are numbered according to the order of left parentheses of capturing groups, starting from 1. The matched substring can be recalled from the resulting array's elements [1], ..., [n] or from the predefined RegExp object's properties $1, ..., $9 .Capturing groups have a performance penalty. If you don't need the matched substring to be recalled, prefer non-capturing parentheses (see below). |
\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, /apple(,)\sorange\1/ matches "apple, orange," in "apple, orange, cherry, peach". |
(?:x) |
Matches x but does not remember the match. These are called non-capturing groups. The matched substring cannot be recalled from the resulting array's elements [1], ..., [n] or from the predefined RegExp object's properties $1, ..., $9 . |
x*? x+? x?? x{n}? x{n,}? x{n,m}? |
Matches the preceding item x like , +, ?, and {...} from above, however the match is the smallest possible match. For example, /<.?>/ matches " Quantifiers without ? are said to be greedy. Those with ? are called "non-greedy". |
Character | Meaning |
---|---|
x* |
Matches the preceding item x 0 or more times. For example, /bo*/ matches "boooo" in "A ghost booooed" and "b" in "A bird warbled", but nothing in "A goat grunted". |
x+ |
Matches the preceding item x 1 or more times. Equivalent to {1,}. For example, /a+/ matches the "a" in "candy" and all the "a"'s in "caaaaaaandy". |
x? |
Matches the preceding item x 0 or 1 time. For example, /e?le?/ matches the "el" in "angel" and the "le" in "angle."If used immediately after any of the quantifiers *, +, ?, or {}, makes the quantifier non-greedy (matching the minimum number of times), as opposed to the default, which is greedy (matching the maximum number of times). |
案例
或
符号时, 起始符中间内容要括起来
const regExpTel: RegExp = /^(\d{3,4}-\d+-\d+|\d{3,4}-\d+)$/;//正确
const regExpTel2: RegExp = /^\d{3,4}-\d+-\d+|\d{3,4}-\d+$/;//错误 匹配到了 /^\d{3,4}-\d+-\d+/ 和 /\d{3,4}-\d+$/
案例
new RegExp()
来写new RegExp
这样的表达式,\s
, \d
这样要写成\\s
, \\d
Note: The
?
character may also be used as a quantifier.
IE don't support Lookbehind Assertions
RegExp.prototype.test()
The test() method executes a search for a match between a regular expression and a specified string. Returns true or false.
RegExp.prototype.exec()
The exec() method executes a search for a match in a specified string. Returns a result array, or null.
Description
If your regular expression uses the "g" flag, you can use the exec() method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression's lastIndex property (test() will also advance the lastIndex property). For example, assume you have this script:
Example
String.prototype.search()
The search() method executes a search for a match between a regular expression and this String object. Return the index of the first match between the regular expression and the given string; if not found, -1.
String.prototype.match()
The match() method retrieves the matches when matching a string against a regular expression.
Parameters
The string against which to match the regular expression.
Return value
If the match succeeds, the exec() method returns an array and updates properties of the regular expression object. The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured. If the match fails, the exec() method returns null.
Description
Examples
reference