版本 532717 / RegExp

  • 版本网址缩略名: JavaScript/Reference/Global_Objects/RegExp
  • 版本标题: RegExp
  • 版本 id: 532717
  • 创建于:
  • 创建者: fishenal
  • 是否是当前版本?
  • 评论

修订内容

概述

创建一个正则表达式对象,用特定的模式匹配文本.

语法

RegExp(pattern [, flags])

/pattern/flags

参数

pattern
正则表达式的文本.
flags

该参数可以是下面几个值的任意组合:

g
全局匹配
i
忽略大小写
m
让开始和结束锚点(^和$)工作在多行模式(也就是,^和$可以匹配字符串中每一行的开始和结束(行是由\n或\r分割的),而不只是整个输入字符串的最开始和最末尾处)
y {{Fx_minversion_inline(3)}} {{non-standard_inline}}
sticky; matches only from the index indicated by the lastIndex property of this regular expression in the target string (and does not attempt to match from any later indexes). This allows the match-only-at-start capabilities of the character "^" to effectively be used at any location in a string by changing the value of the lastIndex property.

描述

当使用结构函数创造正则对象时,通常的字符转意规则(包括特殊字符包含字符串的\)是必要的。比如以下是等价的:

var re = new RegExp("\\w+");
var re = /\w+/;

注意,字面量声明的参数无需引号,而函数声明的参数则需要放在引号里,以下是等价的:

/ab+c/i;
new RegExp("ab+c", "i");

 

Special characters meaning in regular expressions

 

Character Classes
Character Meaning
.

(The dot, the decimal point) matches any single character except the newline characters: \n \r \u2028 or \u2029.

Note that the m multiline flag doesn't change the dot behavior. So to match a pattern accross multiple lines the character set [^] can be used (if you don't mean an old version of IE, of course), it will match any character including newlines.

For example, /.y/ matches "my" and "ay", but not "yes", in "yes make my day".

\d

Matches a digit character in the basic Latin alphabet. Equivalent to [0-9].

For example, /\d/ or /[0-9]/ matches '2' in "B2 is the suite number."

\D

Matches any character that is not a digit in the basic Latin alphabet. Equivalent to [^0-9].

For example, /\D/ or /[^0-9]/ matches 'B' in "B2 is the suite number."

\w

Matches any alphanumeric character from the basic Latin alphabet, including the underscore. Equivalent to [A-Za-z0-9_].

For example, /\w/ matches 'a' in "apple," '5' in "$5.28," and '3' in "3D."

\W

Matches any character that is not a word character from the basic Latin alphabet. Equivalent to [^A-Za-z0-9_].

For example, /\W/ or /[^A-Za-z0-9_]/ matches '%' in "50%."

\s

Matches a single white space character, including space, tab, form feed, line feed and other unicode spaces. Equivalent to [ \t\r\n].

For example, /\s\w*/ matches ' bar' in "foo bar."

\S

Matches a single character other than white space (Not whitespace). Equivalent to [^ \t\r\n].

For example, /\S\w*/ matches 'foo' in "foo bar."

\t Matches a tab.
\r Matches a carriage return.
\n Matches a linefeed.
\v Matches a vertical tab.
\f Matches a form-feed.
[\b] Matches a backspace. (Not to be confused with \b)
\0 Matches a NUL character. Do not follow this with another digit.
\cX

Where X is a letter from A - Z. Matches a control character in a string.

For example, /\cM/ matches control-M in a string.

\xhh Matches the character with the code hh (two hexadecimal digits)
\uhhhh Matches the character with the Unicode value hhhh (four hexadecimal digits).
\

For characters that are usually treated literally, indicates that the next character is special and not to be interpreted literally.

For example, /b/ matches the character 'b'. By placing a backslash in front of b, that is by using /\b/, the character becomes special to mean match a word boundary.

or

For characters that are usually treated specially, indicates that the next character is not special and should be interpreted literally.

For example, * is a special character that means 0 or more occurrences of the preceding character should be matched; for example, /a*/ means match 0 or more "a"s. To match * literally, precede it with a backslash; for example, /a\*/ matches 'a*'.

Character Sets

Character Meaning
[xyz]

A character set. Matches any one of the enclosed characters. You can specify a range of characters by using a hyphen.

For example, [abcd] is the same as [a-d]. They match the 'b' in "brisket" and the 'c' in "chop".

[^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.

For example, [^abc] is the same as [^a-c]. They initially match 'o' in "bacon" and 'h' in "chop."

Boundaries
Character Meaning
^

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 zero-width word boundary, such as between a letter and a space. (Not to be confused with [\b])

For example, /\bno/ matches the 'no' in "at noon"; /ly\b/ matches the 'ly' in "possibly yesterday."

\B

Matches a zero-width non-word boundary, such as between two letters or between two spaces.

For example, /\Bon/ matches 'on' in "at noon", and /ye\B/ matches 'ye' in "possibly yesterday."

Grouping and back references
Character Meaning
(x)

Matches x and remembers the match. These are called capturing parentheses.

For example, /(foo)/ matches and remembers 'foo' in "foo bar." 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." A more complete example follows this table.

(?:x) Matches x but does not remember the match. These are called non-capturing parentheses. The matched substring can not be recalled from the resulting array's elements [1], ..., [n] or from the predefined RegExp object's properties $1, ..., $9.
Quantifiers
Character Meaning
*

Matches the preceding item 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".

+

Matches the preceding item 1 or more times. Equivalent to {1,}.

For example, /a+/ matches the 'a' in "candy" and all the a's in "caaaaaaandy".

*?
+?

Matches like * and + from above, however the match is the smallest possible match.

For example, /".*?"/ matches '"foo"' in '"foo" "bar"' and does not match '"foo" "bar"' as without the ? behind the *.

?

Matches the preceding item 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).

Also used in lookahead assertions, described under (?=)(?!), and (?:) in this table.

x(?=y) Matches x only if x is followed by y. For example, /Jack(?=Sprat)/ matches 'Jack' only if it is followed by 'Sprat'. /Jack(?=Sprat|Frost)/ matches 'Jack' only if it is followed by 'Sprat' or 'Frost'. However, neither 'Sprat' nor 'Frost' is part of the match results.
x(?!y)

Matches x only if x is not followed by y. For example, /\d+(?!\.)/ matches a number only if it is not followed by a decimal point.

/\d+(?!\.)/.exec("3.141") matches 141 but not 3.141.

x|y

Matches either x or y.

For example, /green|red/ matches 'green' in "green apple" and 'red' in "red apple."

{n}

Where n is a positive integer. Matches exactly n occurrences of the preceding item.

For example, /a{2}/ doesn't match the 'a' in "candy," but it matches all of the a's in "caandy," and the first two a's in "caaandy."

{n,}

Where n is a positive integer. Matches at least n occurrences of the preceding item.

For example, /a{2,}/ doesn't match the 'a' in "candy", but matches all of the a's in "caandy" and in "caaaaaaandy."

{n,m}

Where n and m are positive integers. Matches at least n and at most m occurrences of the preceding item.

For example, /a{1,3}/ matches nothing in "cndy", the 'a' in "candy," the two a's in "caandy," and the first three a's in "caaaaaaandy". Notice that when matching "caaaaaaandy", the match is "aaa", even though the original string had more a's in it.

Notes

 

  1. {{endnote("equivalent_s")}}Equivalent to:

    [\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]

  2. {{endnote("equivalent_S")}}Equivalent to:

    [^\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]

 

正则运算中的特殊匹配

Character Meaning
\

这个字符通常代表以字面量来对待,指定其后面的字符为字面量,而不当作字面上来解释。

比如 /b/ 匹配 字母"b", 如果是/\b/ 则代表匹配任何字符边界(空格等)

或者

特殊对待后面的字符,代表下一个字符是特殊的当作字面上来解释

比如,*匹配前字符出现0或多次,/a*/表示a出现0或多次。而如果匹配字符"*“本身,我们需要用到反斜线,如/a\*/匹配 "a*"

^

匹配输入的开始,如果multiline设为真,同时匹配换行后的字符。

比如:/^A/ 无法匹配 "an A",但是可以匹配 "An A"

$

匹配输入的结束。如果multiline设为真,同时匹配换行前的字符。

比如: /t$/不匹配 "eater"中的"t",但是匹配"eat"中的t

*

匹配前项0次或多次。

比如 /bo*/匹配

”A ghost booooed“ 中的"boooo"

"A bird warbled" 中的 "b"

"A goat grunted" 不匹配

+

匹配前项1次或多次。与 {1,}等价。

比如 /a+/ 匹配

"candy" 中的"a"

"caaaaaaaandy"中所有的 "a"

*?
+?

类似 * , + 的匹配前面的项,采用最小可能的匹配。

比如  /".*?"/   匹配 '"foo" "bar"'中的 '"foo"',而不采用?,匹配 '"foo" "bar"'。

?

匹配前项 0-1次。

举例:

/e?le?/ 匹配 "angel" 中的 'el' 以及 "angle." 中的 'le'

如果?用在量词(*+?, {})后,表示量词的非贪婪匹配(匹配最小次数),与之相反的是贪婪匹配(匹配最大次数)

同时问号可使用前置语法,比如 (?=), (?!),和 (?:) ,详情见此表下方。

.

(句号)匹配任意单独字符,换行符除外:\n \r \u2028 or \u2029

[\s\S] 可匹配任意字符,包含换行符。

例子: /.n/ 匹配  "nay, an apple is on the tree" 中的'an' 和'on',但没有 'nay'.

(x)

匹配x并记住这个匹配,叫做捕获括号。

比如,/(foo)/ 匹配并记住 "foo bar." 中的 'foo',匹配的字符串可以在结果数组中通过[1],...,[n]调用,或者从定义过的RegExp对象的属性 $1,...,$9 调用。

捕获括号会损失性能,如果你不需要调用返回数组,则尽可能使用非捕获括号。

(?:x)

匹配x并且不保存匹配。叫做非捕获括号。匹配字符串无法被重新调用。

x(?=y)

匹配x当其位于y后方。

例子: /Jack(?=Sprat)/ 匹配 'Jack' 仅在 'Sprat' 之后。

 /Jack(?=Sprat|Frost)/ 匹配 'Jack'仅在 'Sprat' 或 'Frost'之后。 'Sprat' 和 'Frost' 都不包含在返回结果中。

x(?!y)

匹配x仅不在y之后。

例子: /\d+(?!\.)/ 匹配不在小数点之后的数字。

/\d+(?!\.)/.exec("3.141") matches 141 but not 3.141.

x|y

Matches either x or y.

For example, /green|red/ matches 'green' in "green apple" and 'red' in "red apple."

{n}

Where n is a positive integer. Matches exactly n occurrences of the preceding item.

For example, /a{2}/ doesn't match the 'a' in "candy," but it matches all of the a's in "caandy," and the first two a's in "caaandy."

{n,}

Where n is a positive integer. Matches at least n occurrences of the preceding item.

For example, /a{2,}/ doesn't match the 'a' in "candy", but matches all of the a's in "caandy" and in "caaaaaaandy."

{n,m}

Where n and m are positive integers. Matches at least n and at most m occurrences of the preceding item.

For example, /a{1,3}/ matches nothing in "cndy", the 'a' in "candy," the first two a's in "caandy," and the first three a's in "caaaaaaandy". Notice that when matching "caaaaaaandy", the match is "aaa", even though the original string had more a's in it.

[xyz]

A character set. Matches any one of the enclosed characters. You can specify a range of characters by using a hyphen.

For example, [abcd] is the same as [a-d]. They match the 'b' in "brisket" and the 'c' in "chop".

[^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.

For example, [^abc] is the same as [^a-c]. They initially match 'o' in "bacon" and 'h' in "chop."

[\b] Matches a backspace. (Not to be confused with \b)
\b

Matches a zero-width word boundary, such as between a letter and a space. (Not to be confused with [\b])

For example, /\bno/ matches the 'no' in "at noon"; /ly\b/ matches the 'ly' in "possibly yesterday."

\B

Matches a zero-width non-word boundary, such as between two letters or between two spaces.

For example, /\Bon/ matches 'on' in "at noon", and /ye\B/ matches 'ye' in "possibly yesterday."

\cX

Where X is a letter from A - Z. Matches a control character in a string.

For example, /\cM/ matches control-M in a string.

\d

Matches a digit character in the basic Latin alphabet. Equivalent to [0-9].

Note: In Firefox 2 and earlier, matches a digit character from any alphabet. ({{Bug(378738)}})

For example, /\d/ or /[0-9]/ matches '2' in "B2 is the suite number."

\D

Matches any character that is not a digit in the basic Latin alphabet. Equivalent to [^0-9].

Note: In Firefox 2 and earlier, excludes digit characters from all alphabets. ({{Bug(378738)}})

For example, /\D/ or /[^0-9]/ matches 'B' in "B2 is the suite number."

\f Matches a form-feed.
\n Matches a linefeed.
\r Matches a carriage return.
\s

Matches a single white space character, including space, tab, form feed, line feed and other unicode spaces. Equivalent to [ \t\r\n].

For example, /\s\w*/ matches ' bar' in "foo bar."

\S

Matches a single character other than white space (Not whitespace). Equivalent to [^ \t\r\n].

For example, /\S\w*/ matches 'foo' in "foo bar."

\t Matches a tab.
\v Matches a vertical tab.
\w

Matches any alphanumeric character from the basic Latin alphabet, including the underscore. Equivalent to [A-Za-z0-9_].

For example, /\w/ matches 'a' in "apple," '5' in "$5.28," and '3' in "3D."

\W

Matches any character that is not a word character from the basic Latin alphabet. Equivalent to [^A-Za-z0-9_].

For example, /\W/ or /[^A-Za-z0-9_]/ matches '%' in "50%."

\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." A more complete example follows this table.

\0 Matches a NUL character. Do not follow this with another digit.
\xhh Matches the character with the code hh (two hexadecimal digits)
\uhhhh Matches the character with the Unicode value hhhh (four hexadecimal digits).

The literal notation provides compilation of the regular expression when the expression is evaluated. Use literal notation when the regular expression will remain constant. For example, if you use literal notation to construct a regular expression used in a loop, the regular expression won't be recompiled on each iteration.

The constructor of the regular expression object, for example, new RegExp("ab+c"), provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.

  1. {{endnote("equivalent_s")}}Equivalent to:

    [\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]

  2. {{endnote("equivalent_S")}}Equivalent to:

    [^\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]

Properties

{{Js_see_prototype("RegExp", "Properties")}}
prototype
Allows the addition of properties to all objects.
{{jsOverrides("Function", "Properties", "prototype")}}

Methods

{{Js_see_prototype("RegExp", "Methods")}}

The global RegExp object has no methods of its own, however, it does inherit some methods through the prototype chain.

{{jsOverrides("Function", "Methods", "prototype")}}

RegExp instances

Properties

{{page('en/JavaScript/Reference/Global_Objects/RegExp/prototype','Properties')}}

Methods

{{page('en/JavaScript/Reference/Global_Objects/RegExp/prototype','Methods')}}

Examples

Example: Using a regular expression to change data format

The following script uses the replace method inherited by the String instance to match a name in the format first last and output it in the format last, first. In the replacement text, the script uses $1 and $2 to indicate the results of the corresponding matching parentheses in the regular expression pattern.

var re = /(\w+)\s(\w+)/;
var str = "John Smith";
var newstr = str.replace(re, "$2, $1");
print(newstr);

This displays "Smith, John".

Example: Using a regular expression with the "sticky" flag

This example demonstrates how one could use the sticky flag on regular expressions to match individual lines of multiline input.

var text = "First line\nsecond line";
var regex = /(\S+) line\n?/y;

var match = regex.exec(text);
print(match[1]);  // prints "First"
print(regex.lastIndex); // prints 11

var match2 = regex.exec(text);
print(match2[1]); // prints "Second"
print(regex.lastIndex); // prints "22"

var match3 = regex.exec(text);
print(match3 === null); // prints "true"

One can test at run-time whether the sticky flag is supported, using try { … } catch { … }. For this, either an eval(…) expression or the RegExp(regex-string, flags-string) syntax must be used (since the /regex/flags notation is processed at compile-time, so throws an exception before the catch block is encountered). For example:

var supports_sticky;
try { RegExp('','y'); supports_sticky = true; }
catch(e) { supports_sticky = false; }
alert(supports_sticky); // alerts "false" in Firefox 2, "true" in Firefox 3+

Example: Regular expression and Unicode characters

As mentioned above, \w or \W only matches ASCII based characters; for example, 'a' to 'z', 'A' to 'Z', 0 to 9 and '_'. To match characters from other languagessuch as Cyrillic or Hebrew, use \uhhhh., where "hhhh" is the character's Unicode value in hexadecimal. This example demonstrates how one can separate out Unicode characters from a word.

var text = "Образец text на русском языке";
var regex = /[\u0400-\u04FF]+/g;

var match = regex.exec(text);
print(match[1]);  // prints "Образец"
print(regex.lastIndex);  // prints "7"

var match2 = regex.exec(text);
print(match2[1]);  // prints "на" [did not print "text"]
print(regex.lastIndex);  // prints "15"

// and so on

Here's an external resource for getting the complete Unicode block range for different scripts: Regexp-unicode-block

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

修订版来源

<h2 id="Summary" name="Summary">概述</h2>
<p>创建一个正则表达式对象,用特定的模式匹配文本.</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox">
<code>RegExp(<em>pattern</em> <em>[, flags]</em>)</code>

<code>/<em>pattern</em>/<em>flags</em></code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
 <dt>
  <code>pattern</code></dt>
 <dd>
  正则表达式的文本.</dd>
 <dt>
  <code>flags</code></dt>
 <dd>
  <p>该参数可以是下面几个值的任意组合:</p>
  <dl>
   <dt>
    <code>g</code></dt>
   <dd>
    全局匹配</dd>
   <dt>
    <code>i</code></dt>
   <dd>
    忽略大小写</dd>
   <dt>
    <code>m</code></dt>
   <dd>
    让开始和结束锚点(^和$)工作在多行模式(也就是,^和$可以匹配字符串中每一行的开始和结束(行是由\n或\r分割的),而不只是整个输入字符串的最开始和最末尾处)</dd>
   <dt>
    <code>y</code> {{Fx_minversion_inline(3)}} {{non-standard_inline}}</dt>
   <dd>
    sticky; matches only from the index indicated by the <code>lastIndex</code> property of this regular expression in the target string (and does not attempt to match from any later indexes). This allows the match-only-at-start capabilities of the character "^" to effectively be used at any location in a string by changing the value of the <code>lastIndex</code> property.</dd>
  </dl>
 </dd>
</dl>
<h2 id="Description" name="Description">描述</h2>
<p>当使用结构函数创造正则对象时,通常的字符转意规则(包括特殊字符包含字符串的\)是必要的。比如以下是等价的:</p>
<pre class="brush: js">
var re = new RegExp("\\w+");
var re = /\w+/;
</pre>
<p>注意,字面量声明的参数无需引号,而函数声明的参数则需要放在引号里,以下是等价的:</p>
<pre class="brush: js">
/ab+c/i;
new RegExp("ab+c", "i");
</pre>
<h3 id="Special_characters_in_regular_expressions" name="Special_characters_in_regular_expressions">&nbsp;</h3>
<h2 id="Special_characters_meaning_in_regular_expressions_2" style="line-height: 30px;">Special characters meaning in regular expressions</h2>
<h3 id="Special_characters_in_regular_expressions" name="Special_characters_in_regular_expressions">&nbsp;</h3>
<ul>
 <li><a href="#character-classes">Character Classes</a></li>
 <li><a href="#character-sets">Character Sets</a></li>
 <li><a href="#boundaries">Boundaries</a></li>
 <li><a href="#grouping-back-references">Grouping and back references</a></li>
 <li><a href="#quantifiers">Quantifiers</a></li>
</ul>
<table class="fullwidth-table" style="width: 1162px;">
 <tbody>
  <tr id="character-classes">
   <th colspan="2">Character Classes</th>
  </tr>
  <tr>
   <th>Character</th>
   <th>Meaning</th>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">.</code></td>
   <td>
    <p>(The dot, the decimal point) matches any single character&nbsp;<em>except</em>&nbsp;the newline characters:&nbsp;<code style="font-size: 14px;">\n</code>&nbsp;<code style="font-size: 14px;">\r</code>&nbsp;<code style="font-size: 14px;">\u2028</code>&nbsp;or&nbsp;<code style="font-size: 14px;">\u2029</code>.</p>
    <p>Note that the&nbsp;<code style="font-size: 14px;">m</code>&nbsp;multiline flag doesn't change the dot behavior. So to match a pattern accross multiple lines the character set&nbsp;<code style="font-size: 14px;">[^]</code>&nbsp;can be used (if you don't mean an old version of IE, of course), it will match any character including newlines.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/.y/</code>&nbsp;matches "my" and "ay", but not "yes", in "yes make my day".</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\d</code></td>
   <td>
    <p>Matches a digit character in the basic Latin alphabet. Equivalent to&nbsp;<code style="font-size: 14px;">[0-9]</code>.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/\d/</code>&nbsp;or&nbsp;<code style="font-size: 14px;">/[0-9]/</code>&nbsp;matches '2' in "B2 is the suite number."</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\D</code></td>
   <td>
    <p>Matches any character that is not a digit in the basic Latin alphabet. Equivalent to&nbsp;<code style="font-size: 14px;">[^0-9]</code>.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/\D/</code>&nbsp;or&nbsp;<code style="font-size: 14px;">/[^0-9]/</code>&nbsp;matches 'B' in "B2 is the suite number."</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\w</code></td>
   <td>
    <p>Matches any alphanumeric character from the basic Latin alphabet, including the underscore. Equivalent to&nbsp;<code style="font-size: 14px;">[A-Za-z0-9_]</code>.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/\w/</code>&nbsp;matches 'a' in "apple," '5' in "$5.28," and '3' in "3D."</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\W</code></td>
   <td>
    <p>Matches any character that is not a word character from the basic Latin alphabet. Equivalent to&nbsp;<code style="font-size: 14px;">[^A-Za-z0-9_]</code>.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/\W/</code>&nbsp;or&nbsp;<code style="font-size: 14px;">/[^A-Za-z0-9_]/</code>&nbsp;matches '%' in "50%."</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\s</code></td>
   <td>
    <p>Matches a single white space character, including space, tab, form feed, line feed and other unicode spaces. Equivalent to [ \t\r\n].</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/\s\w*/</code>&nbsp;matches ' bar' in "foo bar."</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\S</code></td>
   <td>
    <p>Matches a single character other than white space (Not whitespace). Equivalent to&nbsp;<code style="font-size: 14px;">[</code><code style="font-size: 14px;">^</code>&nbsp;\t\r\n<code style="font-size: 14px;">]</code>.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/\S\w*/</code>&nbsp;matches 'foo' in "foo bar."</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\t</code></td>
   <td>Matches a tab.</td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\r</code></td>
   <td>Matches a carriage return.</td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\n</code></td>
   <td>Matches a linefeed.</td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\v</code></td>
   <td>Matches a vertical tab.</td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\f</code></td>
   <td>Matches a form-feed.</td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">[\b]</code></td>
   <td>Matches a backspace. (Not to be confused with&nbsp;<code style="font-size: 14px;">\b</code>)</td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\0</code></td>
   <td>Matches a NUL character. Do not follow this with another digit.</td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\c<em>X</em></code></td>
   <td>
    <p>Where&nbsp;<code style="font-size: 14px;"><em>X</em></code>&nbsp;is a letter from A - Z. Matches a control character in a string.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/\cM/</code>&nbsp;matches control-M in a string.</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\x<em>hh</em></code></td>
   <td>Matches the character with the code&nbsp;<code style="font-size: 14px;"><em>hh</em></code>&nbsp;(two hexadecimal digits)</td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\u<em>hhhh</em></code></td>
   <td>Matches the character with the Unicode value&nbsp;<code style="font-size: 14px;"><em>hhhh</em></code>&nbsp;(four hexadecimal digits).</td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\</code></td>
   <td>
    <p>For characters that are usually treated literally, indicates that the next character is special and not to be interpreted literally.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/b/</code>&nbsp;matches the character 'b'. By placing a backslash in front of b, that is by using&nbsp;<code style="font-size: 14px;">/\b/</code>, the character becomes special to mean match a word boundary.</p>
    <p><em>or</em></p>
    <p>For characters that are usually treated specially, indicates that the next character is not special and should be interpreted literally.</p>
    <p>For example, * is a special character that means 0 or more occurrences of the preceding character should be matched; for example,&nbsp;<code style="font-size: 14px;">/a*/</code>&nbsp;means match 0 or more "a"s. To match&nbsp;<code style="font-size: 14px;">*</code>&nbsp;literally, precede it with a backslash; for example,&nbsp;<code style="font-size: 14px;">/a\*/</code>&nbsp;matches 'a*'.</p>
   </td>
  </tr>
 </tbody>
 <tbody>
  <tr id="character-sets">
   <th colspan="2">
    <p>Character Sets</p>
   </th>
  </tr>
  <tr>
   <th>Character</th>
   <th>Meaning</th>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">[xyz]</code></td>
   <td>
    <p>A character set. Matches any one of the enclosed characters. You can specify a range of characters by using a hyphen.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">[abcd]</code>&nbsp;is the same as&nbsp;<code style="font-size: 14px;">[a-d]</code>. They match the 'b' in "brisket" and the 'c' in "chop".</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">[^xyz]</code></td>
   <td>
    <p>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.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">[^abc]</code>&nbsp;is the same as&nbsp;<code style="font-size: 14px;">[^a-c]</code>. They initially match 'o' in "bacon" and 'h' in "chop."</p>
   </td>
  </tr>
 </tbody>
 <tbody>
  <tr id="boundaries">
   <th colspan="2">Boundaries</th>
  </tr>
  <tr>
   <th>Character</th>
   <th>Meaning</th>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">^</code></td>
   <td>
    <p>Matches beginning of input. If the multiline flag is set to true, also matches immediately after a line break character.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/^A/</code>&nbsp;does not match the 'A' in "an A", but does match the first 'A' in "An A."</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">$</code></td>
   <td>
    <p>Matches end of input. If the multiline flag is set to true, also matches immediately before a line break character.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/t$/</code>&nbsp;does not match the 't' in "eater", but does match it in "eat".</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\b</code></td>
   <td>
    <p>Matches a zero-width word boundary, such as between a letter and a space. (Not to be confused with&nbsp;<code style="font-size: 14px;">[\b]</code>)</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/\bno/</code>&nbsp;matches the 'no' in "at noon";&nbsp;<code style="font-size: 14px;">/ly\b/</code>&nbsp;matches the 'ly' in "possibly yesterday."</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\B</code></td>
   <td>
    <p>Matches a zero-width non-word boundary, such as between two letters or between two spaces.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/\Bon/</code>&nbsp;matches 'on' in "at noon", and&nbsp;<code style="font-size: 14px;">/ye\B/</code>&nbsp;matches 'ye' in "possibly yesterday."</p>
   </td>
  </tr>
 </tbody>
 <tbody>
  <tr id="grouping-back-references">
   <th colspan="2">Grouping and back references</th>
  </tr>
  <tr>
   <th>Character</th>
   <th>Meaning</th>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">(<em>x</em>)</code></td>
   <td>
    <p>Matches&nbsp;<code style="font-size: 14px;"><em>x</em></code>&nbsp;and remembers the match. These are called capturing parentheses.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/(foo)/</code>&nbsp;matches and remembers 'foo' in "foo bar." The matched substring can be recalled from the resulting array's elements&nbsp;<code style="font-size: 14px;">[1], ..., [n]</code>&nbsp;or from the predefined&nbsp;<code style="font-size: 14px;">RegExp</code>&nbsp;object's properties&nbsp;<code style="font-size: 14px;">$1, ..., $9</code>.</p>
    <p>Capturing groups have a performance penalty. If you don't need the matched substring to be recalled, prefer non-capturing parentheses (see below).</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">\<em>n</em></code></td>
   <td>
    <p>Where&nbsp;<code style="font-size: 14px;"><em>n</em></code>&nbsp;is a positive integer. A back reference to the last substring matching the n parenthetical in the regular expression (counting left parentheses).</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/apple(,)\sorange\1/</code>&nbsp;matches 'apple, orange,' in "apple, orange, cherry, peach." A more complete example follows this table.</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">(?:<em>x</em>)</code></td>
   <td>Matches&nbsp;<code style="font-size: 14px;"><em>x</em></code>&nbsp;but does not remember the match. These are called non-capturing parentheses. The matched substring can not be recalled from the resulting array's elements&nbsp;<code style="font-size: 14px;">[1], ..., [n]</code>&nbsp;or from the predefined&nbsp;<code style="font-size: 14px;">RegExp</code>&nbsp;object's properties&nbsp;<code style="font-size: 14px;">$1, ..., $9</code>.</td>
  </tr>
 </tbody>
 <tbody>
  <tr id="quantifiers">
   <th colspan="2">Quantifiers</th>
  </tr>
  <tr>
   <th>Character</th>
   <th>Meaning</th>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">*</code></td>
   <td>
    <p>Matches the preceding item 0 or more times.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/bo*/</code>&nbsp;matches 'boooo' in "A ghost booooed" and 'b' in "A bird warbled", but nothing in "A goat grunted".</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">+</code></td>
   <td>
    <p>Matches the preceding item 1 or more times. Equivalent to&nbsp;<code style="font-size: 14px;">{1,}</code>.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/a+/</code>&nbsp;matches the 'a' in "candy" and all the a's in "caaaaaaandy".</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">*?</code><br />
    <code style="font-size: 14px;">+?</code></td>
   <td>
    <p>Matches like&nbsp;<code style="font-size: 14px;">*</code>&nbsp;and&nbsp;<code style="font-size: 14px;">+</code>&nbsp;from above, however the match is the smallest possible match.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/".*?"/</code>&nbsp;matches '"foo"' in '"foo" "bar"' and does not match '"foo" "bar"' as without the&nbsp;<code style="font-size: 14px;">?</code>&nbsp;behind the&nbsp;<code style="font-size: 14px;">*</code>.</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">?</code></td>
   <td>
    <p>Matches the preceding item 0 or 1 time.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/e?le?/</code>&nbsp;matches the 'el' in "angel" and the 'le' in "angle."</p>
    <p>If used immediately after any of the quantifiers&nbsp;<code style="font-size: 14px;">*</code>,&nbsp;<code style="font-size: 14px;">+</code>,&nbsp;<code style="font-size: 14px;">?</code>, or&nbsp;<code style="font-size: 14px;">{}</code>, 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).</p>
    <p>Also used in lookahead assertions, described under&nbsp;<code style="font-size: 14px;">(?=)</code>,&nbsp;<code style="font-size: 14px;">(?!)</code>, and&nbsp;<code style="font-size: 14px;">(?:)</code>&nbsp;in this table.</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;"><em>x</em>(?=<em>y</em>)</code></td>
   <td>Matches&nbsp;<code style="font-size: 14px;"><em>x</em></code>&nbsp;only if&nbsp;<code style="font-size: 14px;"><em>x</em></code>&nbsp;is followed by&nbsp;<code style="font-size: 14px;"><em>y</em></code>. For example,&nbsp;<code style="font-size: 14px;">/Jack(?=Sprat)/</code>&nbsp;matches 'Jack' only if it is followed by 'Sprat'.&nbsp;<code style="font-size: 14px;">/Jack(?=Sprat|Frost)/</code>&nbsp;matches 'Jack' only if it is followed by 'Sprat' or 'Frost'. However, neither 'Sprat' nor 'Frost' is part of the match results.</td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;"><em>x</em>(?!<em>y</em>)</code></td>
   <td>
    <p>Matches&nbsp;<code style="font-size: 14px;"><em>x</em></code>&nbsp;only if&nbsp;<code style="font-size: 14px;"><em>x</em></code>&nbsp;is not followed by&nbsp;<code style="font-size: 14px;"><em>y</em></code>. For example,&nbsp;<code style="font-size: 14px;">/\d+(?!\.)/</code>&nbsp;matches a number only if it is not followed by a decimal point.</p>
    <p><code style="font-size: 14px;">/\d+(?!\.)/.exec("3.141")</code>&nbsp;matches 141 but not 3.141.</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;"><em>x</em>|<em>y</em></code></td>
   <td>
    <p>Matches either&nbsp;<code style="font-size: 14px;"><em>x</em></code>&nbsp;or&nbsp;<code style="font-size: 14px;"><em>y</em></code>.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/green|red/</code>&nbsp;matches 'green' in "green apple" and 'red' in "red apple."</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">{<em>n</em>}</code></td>
   <td>
    <p>Where&nbsp;<code style="font-size: 14px;"><em>n</em></code>&nbsp;is a positive integer. Matches exactly&nbsp;<code style="font-size: 14px;"><em>n</em></code>&nbsp;occurrences of the preceding item.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/a{2}/</code>&nbsp;doesn't match the 'a' in "candy," but it matches all of the a's in "caandy," and the first two a's in "caaandy."</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">{<em>n</em>,}</code></td>
   <td>
    <p>Where&nbsp;<code style="font-size: 14px;"><em>n</em></code>&nbsp;is a positive integer. Matches at least&nbsp;<code style="font-size: 14px;"><em>n</em></code>&nbsp;occurrences of the preceding item.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/a{2,}/</code>&nbsp;doesn't match the 'a' in "candy", but matches all of the a's in "caandy" and in "caaaaaaandy."</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">{<em>n</em>,<em>m</em>}</code></td>
   <td>
    <p>Where&nbsp;<code style="font-size: 14px;"><em>n</em></code>&nbsp;and&nbsp;<code style="font-size: 14px;"><em>m</em></code>&nbsp;are positive integers. Matches at least&nbsp;<code style="font-size: 14px;"><em>n</em></code>&nbsp;and at most&nbsp;<code style="font-size: 14px;"><em>m</em></code>&nbsp;occurrences of the preceding item.</p>
    <p>For example,&nbsp;<code style="font-size: 14px;">/a{1,3}/</code>&nbsp;matches nothing in "cndy", the 'a' in "candy," the two a's in "caandy," and the first three a's in "caaaaaaandy". Notice that when matching "caaaaaaandy", the match is "aaa", even though the original string had more a's in it.</p>
   </td>
  </tr>
 </tbody>
</table>
<h3 id="Notes_2" style="line-height: 24px;">Notes</h3>
<h3 id="Special_characters_in_regular_expressions" name="Special_characters_in_regular_expressions">&nbsp;</h3>
<ol>
 <li>{{endnote("equivalent_s")}}Equivalent to:
  <p><code style="font-size: 14px;">[\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]</code></p>
 </li>
 <li>{{endnote("equivalent_S")}}Equivalent to:
  <p><code style="font-size: 14px;">[^\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]</code></p>
 </li>
</ol>
<h3 id="Special_characters_in_regular_expressions" name="Special_characters_in_regular_expressions">&nbsp;</h3>
<h3 id="Special_characters_in_regular_expressions" name="Special_characters_in_regular_expressions">正则运算中的特殊匹配</h3>
<table class="fullwidth-table">
 <tbody>
  <tr>
   <td class="header">Character</td>
   <td class="header">Meaning</td>
  </tr>
  <tr>
   <td><code>\</code></td>
   <td>
    <p>这个字符通常代表以字面量来对待,指定其后面的字符为字面量,而<span style="color:#ff0000;">不当作</span>字面上来解释。</p>
    <p>比如 /b/ 匹配 字母"b", 如果是/\b/ 则代表匹配任何字符边界(空格等)</p>
    <p>或者</p>
    <p>特殊对待后面的字符,代表下一个字符是特殊的<span style="color:#ff0000;">当作</span>字面上来解释</p>
    <p>比如,*匹配前字符出现0或多次,/a*/表示a出现0或多次。而如果匹配字符"*“本身,我们需要用到反斜线,如/a\*/匹配 "a*"</p>
   </td>
  </tr>
  <tr>
   <td><code>^</code></td>
   <td>
    <p>匹配输入的开始,如果multiline设为真,同时匹配换行后的字符。</p>
    <p>比如:/^A/ 无法匹配 "an A",但是可以匹配 "An A"</p>
   </td>
  </tr>
  <tr>
   <td><code>$</code></td>
   <td>
    <p>匹配输入的结束。如果multiline设为真,同时匹配换行前的字符。</p>
    <p>比如:<span style="line-height: 1.5;">&nbsp;</span><code style="font-size: 14px;">/t$/不匹配 "eater"中的"t",但是匹配"eat"中的t</code></p>
   </td>
  </tr>
  <tr>
   <td><code>*</code></td>
   <td>
    <p>匹配前项0次或多次。</p>
    <p>比如 /bo*/匹配</p>
    <p>”A ghost booooed“ 中的"boooo"</p>
    <p>"A bird warbled" 中的 "b"</p>
    <p>"A goat grunted" 不匹配</p>
   </td>
  </tr>
  <tr>
   <td><code>+</code></td>
   <td>
    <p>匹配前项1次或多次。与 {1,}等价。</p>
    <p>比如 /a+/ 匹配</p>
    <p>"candy" 中的"a"</p>
    <p>"caaaaaaaandy"中所有的 "a"</p>
   </td>
  </tr>
  <tr>
   <td><code>*?</code><br />
    <code>+?</code></td>
   <td>
    <p>类似 * , + 的匹配前面的项,采用最小可能的匹配。</p>
    <p>比如 &nbsp;<code>/".*?"/</code>&nbsp; &nbsp;匹配 '"foo" "bar"'中的&nbsp;'"foo"',而不采用?,匹配&nbsp;'"foo" "bar"'。</p>
   </td>
  </tr>
  <tr>
   <td><code>?</code></td>
   <td>
    <p>匹配前项 0-1次。</p>
    <p>举例:</p>
    <p><code style="font-size: 14px;">/e?le?/</code><span style="line-height: 1.5;">&nbsp;匹配 "angel" 中的&nbsp;</span><span style="line-height: 1.5;">'el' 以及</span><span style="line-height: 1.5;">&nbsp;"angle." 中的&nbsp;</span><span style="line-height: 1.5;">'le'</span></p>
    <p>如果?用在量词(<code style="font-size: 14px;">*</code><span style="line-height: 1.5;">,&nbsp;</span><code style="font-size: 14px;">+</code><span style="line-height: 1.5;">,&nbsp;</span><code style="font-size: 14px;">?</code><span style="line-height: 1.5;">, </span><code style="font-size: 14px;">{}</code><span style="line-height: 1.5;">)后,表示量词的非贪婪匹配(匹配最小次数),与之相反的是贪婪匹配(匹配最大次数)</span></p>
    <p>同时问号可使用前置语法,比如<span style="line-height: 1.5;">&nbsp;</span><code style="font-size: 14px;">(?=)</code><span style="line-height: 1.5;">, </span><code style="font-size: 14px;">(?!)</code><span style="line-height: 1.5;">,和&nbsp;</span><code style="font-size: 14px;">(?:)</code><span style="line-height: 1.5;">&nbsp;,详情见此表下方。</span></p>
   </td>
  </tr>
  <tr>
   <td><code>.</code></td>
   <td>
    <p>(句号)匹配任意单独字符,换行符除外:\n \r \u2028 or \u2029</p>
    <p>*&nbsp;<code style="font-size: 14px;">[\s\S]</code><span style="line-height: 1.5;">&nbsp;可匹配任意字符,包含换行符。</span></p>
    <p><span style="line-height: 1.5;">例子:</span><span style="line-height: 1.5;">&nbsp;</span><code style="font-size: 14px;">/.n/</code><span style="line-height: 1.5;">&nbsp;匹配 &nbsp;"nay, an apple is on the tree" 中的</span><span style="line-height: 1.5;">'an' 和'on',但没有</span><span style="line-height: 1.5;">&nbsp;'nay'.</span></p>
   </td>
  </tr>
  <tr>
   <td><code>(<em>x</em>)</code></td>
   <td>
    <p>匹配x并记住这个匹配,叫做捕获括号。</p>
    <p>比如,<span style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;">/(foo)/ 匹配并记住&nbsp;</span><span style="line-height: 1.5;">"foo bar." 中的&nbsp;</span><span style="line-height: 1.5;">'foo',匹配的字符串可以在结果数组中通过[1],...,[n]调用,或者从定义过的RegExp对象的属性 $1,...,$9 调用。</span></p>
    <p>捕获括号会损失性能,如果你不需要调用返回数组,则尽可能使用非捕获括号。</p>
   </td>
  </tr>
  <tr>
   <td><code>(?:<em>x</em>)</code></td>
   <td>
    <p>匹配x并且不保存匹配。叫做非捕获括号。匹配字符串无法被重新调用。</p>
   </td>
  </tr>
  <tr>
   <td><code><em>x</em>(?=<em>y</em>)</code></td>
   <td>
    <p>匹配x当其位于y后方。</p>
    <p>例子:<span style="line-height: 1.5;">&nbsp;</span><code style="font-size: 14px;">/Jack(?=Sprat)/</code><span style="line-height: 1.5;">&nbsp;匹配&nbsp;'Jack' 仅在 'Sprat' 之后。</span></p>
    <p><span style="line-height: 1.5;">&nbsp;</span><code style="font-size: 14px;">/Jack(?=Sprat|Frost)/</code><span style="line-height: 1.5;">&nbsp;匹配&nbsp;'Jack'仅在&nbsp;'Sprat' 或 'Frost'之后。 'Sprat' 和 'Frost' 都不包含在返回结果中。</span></p>
   </td>
  </tr>
  <tr>
   <td><code><em>x</em>(?!<em>y</em>)</code></td>
   <td>
    <p>匹配x仅不在y之后。</p>
    <p>例子:<span style="line-height: 1.5;">&nbsp;</span><code style="font-size: 14px;">/\d+(?!\.)/</code><span style="line-height: 1.5;">&nbsp;匹配不在小数点之后的数字。</span></p>
    <p><code>/\d+(?!\.)/.exec("3.141")</code> matches 141 but not 3.141.</p>
   </td>
  </tr>
  <tr>
   <td><code><em>x</em>|<em>y</em></code></td>
   <td>
    <p>Matches either <code><em>x</em></code> or <code><em>y</em></code>.</p>
    <p>For example, <code>/green|red/</code> matches 'green' in "green apple" and 'red' in "red apple."</p>
   </td>
  </tr>
  <tr>
   <td><code>{<em>n</em>}</code></td>
   <td>
    <p>Where <code><em>n</em></code> is a positive integer. Matches exactly <code><em>n</em></code> occurrences of the preceding item.</p>
    <p>For example, <code>/a{2}/</code> doesn't match the 'a' in "candy," but it matches all of the a's in "caandy," and the first two a's in "caaandy."</p>
   </td>
  </tr>
  <tr>
   <td><code>{<em>n</em>,}</code></td>
   <td>
    <p>Where <code><em>n</em></code> is a positive integer. Matches at least <code><em>n</em></code> occurrences of the preceding item.</p>
    <p>For example, <code>/a{2,}/</code> doesn't match the 'a' in "candy", but matches all of the a's in "caandy" and in "caaaaaaandy."</p>
   </td>
  </tr>
  <tr>
   <td><code>{<em>n</em>,<em>m</em>}</code></td>
   <td>
    <p>Where <code><em>n</em></code> and <code><em>m</em></code> are positive integers. Matches at least <code><em>n</em></code> and at most <code><em>m</em></code> occurrences of the preceding item.</p>
    <p>For example, <code>/a{1,3}/</code> matches nothing in "cndy", the 'a' in "candy," the first two a's in "caandy," and the first three a's in "caaaaaaandy". Notice that when matching "caaaaaaandy", the match is "aaa", even though the original string had more a's in it.</p>
   </td>
  </tr>
  <tr>
   <td><code>[xyz]</code></td>
   <td>
    <p>A character set. Matches any one of the enclosed characters. You can specify a range of characters by using a hyphen.</p>
    <p>For example, <code>[abcd]</code> is the same as <code>[a-d]</code>. They match the 'b' in "brisket" and the 'c' in "chop".</p>
   </td>
  </tr>
  <tr>
   <td><code>[^xyz]</code></td>
   <td>
    <p>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.</p>
    <p>For example, <code>[^abc]</code> is the same as <code>[^a-c]</code>. They initially match 'o' in "bacon" and 'h' in "chop."</p>
   </td>
  </tr>
  <tr>
   <td><code>[\b]</code></td>
   <td>Matches a backspace. (Not to be confused with <code>\b</code>)</td>
  </tr>
  <tr>
   <td><code>\b</code></td>
   <td>
    <p>Matches a zero-width word boundary, such as between a letter and a space. (Not to be confused with <code>[\b]</code>)</p>
    <p>For example, <code>/\bno/</code> matches the 'no' in "at noon"; <code>/ly\b/</code> matches the 'ly' in "possibly yesterday."</p>
   </td>
  </tr>
  <tr>
   <td><code>\B</code></td>
   <td>
    <p>Matches a zero-width non-word boundary, such as between two letters or between two spaces.</p>
    <p>For example, <code>/\Bon/</code> matches 'on' in "at noon", and <code>/ye\B/</code> matches 'ye' in "possibly yesterday."</p>
   </td>
  </tr>
  <tr>
   <td><code>\c<em>X</em></code></td>
   <td>
    <p>Where <code><em>X</em></code> is a letter from A - Z. Matches a control character in a string.</p>
    <p>For example, <code>/\cM/</code> matches control-M in a string.</p>
   </td>
  </tr>
  <tr>
   <td><code>\d</code></td>
   <td>
    <p>Matches a digit character in the basic Latin alphabet. Equivalent to <code>[0-9]</code>.</p>
    <p><strong>Note</strong>: In Firefox 2 and earlier, matches a digit character from any alphabet. ({{Bug(378738)}})</p>
    <p>For example, <code>/\d/</code> or <code>/[0-9]/</code> matches '2' in "B2 is the suite number."</p>
   </td>
  </tr>
  <tr>
   <td><code>\D</code></td>
   <td>
    <p>Matches any character that is not a digit in the basic Latin alphabet. Equivalent to <code>[^0-9]</code>.</p>
    <p><strong>Note</strong>: In Firefox 2 and earlier, excludes digit characters from all alphabets. ({{Bug(378738)}})</p>
    <p>For example, <code>/\D/</code> or <code>/[^0-9]/</code> matches 'B' in "B2 is the suite number."</p>
   </td>
  </tr>
  <tr>
   <td><code>\f</code></td>
   <td>Matches a form-feed.</td>
  </tr>
  <tr>
   <td><code>\n</code></td>
   <td>Matches a linefeed.</td>
  </tr>
  <tr>
   <td><code>\r</code></td>
   <td>Matches a carriage return.</td>
  </tr>
  <tr>
   <td><code>\s</code></td>
   <td>
    <p>Matches a single white space character, including space, tab, form feed, line feed and other unicode spaces. Equivalent to [ \t\r\n].</p>
    <p>For example, <code>/\s\w*/</code> matches ' bar' in "foo bar."</p>
   </td>
  </tr>
  <tr>
   <td><code>\S</code></td>
   <td>
    <p>Matches a single character other than white space (Not whitespace). Equivalent to <code>[</code><code>^</code> \t\r\n<code>]</code>.</p>
    <p>For example, <code>/\S\w*/</code> matches 'foo' in "foo bar."</p>
   </td>
  </tr>
  <tr>
   <td><code>\t</code></td>
   <td>Matches a tab.</td>
  </tr>
  <tr>
   <td><code>\v</code></td>
   <td>Matches a vertical tab.</td>
  </tr>
  <tr>
   <td><code>\w</code></td>
   <td>
    <p>Matches any alphanumeric character from the basic Latin alphabet, including the underscore. Equivalent to <code>[A-Za-z0-9_]</code>.</p>
    <p>For example, <code>/\w/</code> matches 'a' in "apple," '5' in "$5.28," and '3' in "3D."</p>
   </td>
  </tr>
  <tr>
   <td><code>\W</code></td>
   <td>
    <p>Matches any character that is not a word character from the basic Latin alphabet. Equivalent to <code>[^A-Za-z0-9_]</code>.</p>
    <p>For example, <code>/\W/</code> or <code>/[^A-Za-z0-9_]/</code> matches '%' in "50%."</p>
   </td>
  </tr>
  <tr>
   <td><code>\<em>n</em></code></td>
   <td>
    <p>Where <code><em>n</em></code> is a positive integer. A back reference to the last substring matching the n parenthetical in the regular expression (counting left parentheses).</p>
    <p>For example, <code>/apple(,)\sorange\1/</code> matches 'apple, orange,' in "apple, orange, cherry, peach." A more complete example follows this table.</p>
   </td>
  </tr>
  <tr>
   <td><code>\0</code></td>
   <td>Matches a NUL character. Do not follow this with another digit.</td>
  </tr>
  <tr>
   <td><code>\x<em>hh</em></code></td>
   <td>Matches the character with the code <code><em>hh</em></code> (two hexadecimal digits)</td>
  </tr>
  <tr>
   <td><code>\u<em>hhhh</em></code></td>
   <td>Matches the character with the Unicode value <code><em>hhhh</em></code> (four hexadecimal digits).</td>
  </tr>
 </tbody>
</table>
<p>The literal notation provides compilation of the regular expression when the expression is evaluated. Use literal notation when the regular expression will remain constant. For example, if you use literal notation to construct a regular expression used in a loop, the regular expression won't be recompiled on each iteration.</p>
<p>The constructor of the regular expression object, for example, <code>new RegExp("ab+c")</code>, provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.</p>
<ol>
 <li>{{endnote("equivalent_s")}}Equivalent to:
  <p><code>[\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]</code></p>
 </li>
 <li>{{endnote("equivalent_S")}}Equivalent to:
  <p><code>[^\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]</code></p>
 </li>
</ol>
<h3 id="Properties" name="Properties">Properties</h3>
<div>
 {{Js_see_prototype("RegExp", "Properties")}}</div>
<dl>
 <dt>
  <a href="/en-US/docs/JavaScript/Reference/Global_Objects/RegExp/prototype" title="JavaScript/Reference/Global_Objects/RegExp/prototype">prototype</a></dt>
 <dd>
  Allows the addition of properties to all objects.</dd>
</dl>
<div>
 {{jsOverrides("Function", "Properties", "prototype")}}</div>
<h3 id="Methods" name="Methods">Methods</h3>
<div>
 {{Js_see_prototype("RegExp", "Methods")}}</div>
<p>The global <code>RegExp</code> object has no methods of its own, however, it does inherit some methods through the prototype chain.</p>
<div>
 {{jsOverrides("Function", "Methods", "prototype")}}</div>
<h2 id="RegExp_instances"><code>RegExp</code> instances</h2>
<h3 id="RegExp_instances-Properties" name="RegExp_instances-Properties">Properties</h3>
<div>
 {{page('en/JavaScript/Reference/Global_Objects/RegExp/prototype','Properties')}}</div>
<h3 id="RegExp_instances-Methods" name="RegExp_instances-Methods">Methods</h3>
<div>
 {{page('en/JavaScript/Reference/Global_Objects/RegExp/prototype','Methods')}}</div>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_a_regular_expression_to_change_data_format" name="Example:_Using_a_regular_expression_to_change_data_format">Example: Using a regular expression to change data format</h3>
<p>The following script uses the <a href="/en-US/docs/JavaScript/Reference/Global_Objects/String/replace" title="JavaScript/Reference/Global_Objects/String/replace">replace</a> method inherited by the <a href="/en-US/docs/JavaScript/Reference/Global_Objects/String" title="JavaScript/Reference/Global_Objects/String">String</a> instance to match a name in the format <em>first last</em> and output it in the format <em>last</em>, <em>first</em>. In the replacement text, the script uses <code>$1</code> and <code>$2</code> to indicate the results of the corresponding matching parentheses in the regular expression pattern.</p>
<pre class="brush: js">
var re = /(\w+)\s(\w+)/;
var str = "John Smith";
var newstr = str.replace(re, "$2, $1");
print(newstr);</pre>
<p>This displays "Smith, John".</p>
<h3 id="Example:_Using_a_regular_expression_with_the_sticky_flag" name="Example:_Using_a_regular_expression_with_the_sticky_flag">Example: Using a regular expression with the "sticky" flag</h3>
<p>This example demonstrates how one could use the sticky flag on regular expressions to match individual lines of multiline input.</p>
<pre class="brush: js">
var text = "First line\nsecond line";
var regex = /(\S+) line\n?/y;

var match = regex.exec(text);
print(match[1]);  // prints "First"
print(regex.lastIndex); // prints 11

var match2 = regex.exec(text);
print(match2[1]); // prints "Second"
print(regex.lastIndex); // prints "22"

var match3 = regex.exec(text);
print(match3 === null); // prints "true"</pre>
<p>One can test at run-time whether the sticky flag is supported, using <code>try { … } catch { … }</code>. For this, either an <code>eval(…)</code> expression or the <code>RegExp(<var>regex-string</var>, <var>flags-string</var>)</code> syntax must be used (since the <code>/<var>regex</var>/<var>flags</var></code> notation is processed at compile-time, so throws an exception before the <code>catch</code> block is encountered). For example:</p>
<pre class="brush: js">
var supports_sticky;
try { RegExp('','y'); supports_sticky = true; }
catch(e) { supports_sticky = false; }
alert(supports_sticky); // alerts "false" in Firefox 2, "true" in Firefox 3+</pre>
<h3 id="Browser_Compatibility" name="Browser_Compatibility">Example: Regular expression and Unicode characters</h3>
<p>As mentioned above, <code>\w</code> or <code>\W</code> only matches ASCII based characters; for example, 'a' to 'z', 'A' to 'Z', 0 to 9 and '_'. To match characters from other languagessuch as Cyrillic or Hebrew, use <code>\uhhhh</code>., where "hhhh" is the character's Unicode value in hexadecimal. This example demonstrates how one can separate out Unicode characters from a word.</p>
<pre class="brush: js">
var text = "Образец text на русском языке";
var regex = /[\u0400-\u04FF]+/g;

var match = regex.exec(text);
print(match[1]);  // prints "Образец"
print(regex.lastIndex);  // prints "7"

var match2 = regex.exec(text);
print(match2[1]);  // prints "на" [did not print "text"]
print(regex.lastIndex);  // prints "15"

// and so on</pre>
<p>Here's an external resource for getting the complete Unicode block range for different scripts: <a href="http://kourge.net/projects/regexp-unicode-block" title="http://kourge.net/projects/regexp-unicode-block">Regexp-unicode-block</a></p>
<h2 id="Browser_Compatibility" name="Browser_Compatibility">Browser compatibility</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari (WebKit)</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Phone</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li><a href="/en-US/docs/JavaScript/Guide/Regular_Expressions" title="JavaScript/Guide/Regular_Expressions">Regular Expressions</a> chapter in the <a href="/en-US/docs/JavaScript/Guide" title="JavaScript/Guide">JavaScript Guide</a></li>
</ul>
恢复到这个版本