aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/regexp/regexp/index.html
blob: 8ade9bd6eb8732fd1f6274156a632f13370aaee9 (plain)
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">/<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>正则表达式的文本。从ES5开始,这也可以是另一个<code>RegExp</code>对象或文字(仅用于两个RegExp构造函数符号)。模式可以包含特殊字符<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Using_special_characters">special characters</a>来匹配比字面值字符串更广泛的值范围。</dd>
 <dt><code><var>flags</var></code></dt>
 <dd>
 <p>如果指定, <code><var>flags</var></code> 是包含要添加的标志的字符串。</p>

 <p>或者,如果为模式提供了一个对象,flags字符串将替换该对象的任何标志(并且<code>lastIndex</code>将重置为0)(从ES2015开始)。</p>
 
 <p>如果没有指定<code><var>flags</var></code>并且提供了一个正则表达式对象,则该对象的flags(和lastIndex值)将被复制。</p>

 <p><code>flags</code> 可包含下列任何字符的组合:</p>

 <dl>
  <dt><code>g</code> (全局匹配)</dt>
  <dd>找到所有的匹配,而不是在第一个匹配之后停止。</dd>
  <dt><code>i</code> (忽略大小写)</dt>
  <dd>如果<code>u</code>标志也被启用,使用Unicode大小写折叠。</dd>
  <dt><code>m</code> (多行匹配)</dt>
  <dd>将开始和结束字符(<code>^</code> and <code>$</code>)视为在多行上工作。换句话说,匹配每一行的开头或结尾<em>each</em> line (由<code>\n</code>或者<code>\r</code> 分隔),而不仅仅是整个输入字符串的开头或结尾。</dd>
  <dt><code>s</code> (点号匹配所有字符)</dt>
  <dd>允许<code>.</code> 去匹配新的行</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>这里有两种方法创建<code>RegExp</code> 对象: <em>字面量</em><em>构造函数</em>.</p>

<ul>
 <li><strong>文字符号的</strong> 参数用斜杠括起来,不使用引号。</li>
 <li><strong>构造函数的</strong> 参数不包含在斜杠之间,但使用引号。</li>
</ul>

<p>以下三个表达式创建相同的正则表达式:</p>

<pre class="brush: js">/ab+c/i
new RegExp(/ab+c/, 'i') // 字面量
new RegExp('ab+c', 'i') // 构造函数
</pre>

<p>当表达式被求值时,文字表示法会导致对正则表达式的编译。当正则表达式保持不变时,请使用字面量表示法。例如,如果使用字面量表示法来构造循环中使用的正则表达式,则不会在每次迭代时重新编译正则表达式。</p>

<p>正则表达式对象的构造函数—例如,new RegExp('ab+c')—会导致正则表达式的运行时编译。当您知道正则表达式模式将发生变化时,或者您不知道该模式,但正在从其他来源(如用户输入)获取它时,请使用构造函数。</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">技术规范</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>
<li><a href="/zh-CN/docs/Web/JavaScript/Guide">JavaScript指南中的</a></li>
<li><a href="/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions">正则表达式一节</a></li>
 <li>{{jsxref("String.prototype.match()")}}</li>
 <li>{{jsxref("String.prototype.replace()")}}</li>
</ul>