1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
---
title: RegExp() constructor
slug: Web/JavaScript/Reference/Global_Objects/RegExp/RegExp
translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/RegExp
---
<div>{{JSRef}}</div>
<p><strong><code>RegExp</code></strong> 用于创建正则表达式对象,该对象用于将文本与一个模式匹配</p>
<p>阅读<a href="/zh-CN/docs/Web/JavaScript/Guide" title="JavaScript/Guide">JavaScript指南</a>中的<a href="/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions" title="JavaScript/Guide/Regular_Expressions">正则表达式</a>一节以了解正则表达式。</p>
<div>{{EmbedInteractiveExample("pages/js/regexp-constructor.html")}}</div>
<h2 id="语法">语法</h2>
<p>可以使用字面量、构造函数和工厂方法来创建正则表达式</p>
<pre class="syntaxbox notranslate">/<var>pattern</var>/<var>flags</var>
new RegExp(<var>pattern</var>[, <var>flags</var>])
RegExp(<var>pattern</var>[, <var>flags</var>])
</pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code><var>pattern</var></code></dt>
<dd>The text of the regular expression.</dd>
<dd>As of ES5, this can also be another <code>RegExp</code> object or literal (for the two RegExp constructor notations only). Patterns may include <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Using_special_characters">special characters</a> to match a wider range of values than would a literal string. </dd>
<dt><code><var>flags</var></code></dt>
<dd>
<p>If specified, <code><var>flags</var></code> is a string that contains the flags to add.</p>
<p>Alternatively, if an object is supplied for the pattern, the <code><var>flags</var></code> string will replace any of that object's flags (and <code>lastIndex</code> will be reset to <code>0</code>) (as of ES2015).</p>
<p>If <code><var>flags</var></code> is not specified and a regular expressions object is supplied, that object's flags (and <code>lastIndex</code> value) will be copied over.</p>
<p><code>flags</code> may contain any combination of the following characters:</p>
<dl>
<dt><code>g</code> (全局匹配)</dt>
<dd>Find all matches rather than stopping after the first match.</dd>
<dt><code>i</code> (忽略大小写)</dt>
<dd>If <code>u</code> flag is also enabled, use Unicode case folding.</dd>
<dt><code>m</code> (多行匹配)</dt>
<dd>Treat beginning and end characters (<code>^</code> and <code>$</code>) as working over multiple lines. In other words, match the beginning or end of <em>each</em> line (delimited by <code>\n</code> or <code>\r</code>), not only the very beginning or end of the whole input string.</dd>
<dt><code>s</code> (点号匹配所有字符)</dt>
<dd>Allows <code>.</code> to match newlines.</dd>
<dt><code>u</code> (unicode)</dt>
<dd>Treat <code><var>pattern</var></code> as a sequence of Unicode code points. (See also <a href="/en-US/docs/Web/API/DOMString/Binary">Binary strings</a>).</dd>
<dt><code>y</code> (sticky,粘性匹配)</dt>
<dd>Matches only from the index indicated by the <code>lastIndex</code> property of this regular expression in the target string. Does not attempt to match from any later indexes.</dd>
</dl>
</dd>
</dl>
<h2 id="例子">例子</h2>
<h3 id="字面量和构造函数">字面量和构造函数</h3>
<p>There are two ways to create a <code>RegExp</code> object: a <em>literal notation</em> and a <em>constructor</em>.</p>
<ul>
<li><strong>The literal notation's</strong> parameters are enclosed between slashes and do not use quotation marks.</li>
<li><strong>The constructor function's</strong> parameters are not enclosed between slashes but do use quotation marks.</li>
</ul>
<p>The following three expressions create the same regular expression:</p>
<pre class="brush: js notranslate">/ab+c/i
new RegExp(/ab+c/, 'i') // literal notation
new RegExp('ab+c', 'i') // constructor
</pre>
<p>The literal notation results in 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>—results in 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>
<h2 id="规范">规范</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-regexp-constructor', 'RegExp constructor')}}</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>
<p>{{Compat("javascript.builtins.RegExp.RegExp")}}</p>
</div>
<h2 id="参见">参见</h2>
<ul>
<a href="/zh-CN/docs/Web/JavaScript/Guide" title="JavaScript/Guide">JavaScript指南</a>中的<a href="/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions" title="JavaScript/Guide/Regular_Expressions">正则表达式</a>一节
<li>{{jsxref("String.prototype.match()")}}</li>
<li>{{jsxref("String.prototype.replace()")}}</li>
</ul>
|