blob: 6365649479f3aab333029865487fb848035a39f2 (
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
|
---
title: 'SyntaxError: invalid regular expression flag "x"'
slug: Web/JavaScript/Reference/Errors/Bad_regexp_flag
tags:
- 语法错误
- 错误
translation_of: Web/JavaScript/Reference/Errors/Bad_regexp_flag
---
<div>{{jsSidebar("Errors")}}</div>
<h2 id="信息">信息</h2>
<pre class="syntaxbox">SyntaxError: invalid regular expression flag "x" (Firefox)
SyntaxError: Invalid regular expression flags (Chrome)
</pre>
<h2 id="错误类型">错误类型</h2>
<p>{{jsxref("SyntaxError","语法错误")}}.</p>
<h2 id="什么地方出错了">什么地方出错了?</h2>
<p>在代码中出现了无效的正则表达式的标记。在一个正则表达式字面量中,由闭合的两条斜线组成一个模式,(正则表达式的)标记定义在第二个(斜线)标记之后。他们也可以通过{{jsxref("RegExp", "正则表达式")}} 对象的构造函数(第二个参数)来定义。正则表达式的标记可以单独或者任意次序的组合使用,但ECMAScript只规定了五个。</p>
<p>要使正则表达式包含标记,使用此语法:</p>
<pre class="brush: js">var re = /pattern/flags;
</pre>
<p>或</p>
<pre class="brush: js">var re = new RegExp('pattern', 'flags');</pre>
<table class="standard-table">
<caption>正则表达式标记</caption>
<thead>
<tr>
<th scope="col">标记</th>
<th scope="col">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>g</code></td>
<td>整体检索.</td>
</tr>
<tr>
<td>i</td>
<td>忽略大小写检索.</td>
</tr>
<tr>
<td>m</td>
<td>多行检索.</td>
</tr>
<tr>
<td>u</td>
<td>Unicode; 将模式视为Unicode码点的序列</td>
</tr>
<tr>
<td>y</td>
<td>sticky 检索将从目标字符串的当前位置开始匹配。参阅{{jsxref("RegExp.sticky", "sticky")}}</td>
</tr>
</tbody>
</table>
<h2 id="示例">示例</h2>
<p>只有5个有效的正则表达式标记。</p>
<pre class="brush: js example-bad">/foo/bar;
// SyntaxError: invalid regular expression flag "b"
</pre>
<p>你打算创建一个正则表达式吗?一个包含两条斜线的表达式被解释为一个正则表达式的字面量。</p>
<pre class="brush: js example-bad">let obj = {
url: /docs/Web
};
// SyntaxError: invalid regular expression flag "W"
</pre>
<p>还是你想创建一个字符串呢?添加单引号或双引号创建一个字符串字面量。</p>
<pre class="brush: js example-good">let obj = {
url: '/docs/Web'
};</pre>
<h3 id="有效的正则表达式标记">有效的正则表达式标记</h3>
<p>在JavaScript中允许的五个有效的正则表达式标记,参阅上表。</p>
<pre class="brush: js example-good">/foo/g;
/foo/gim;
/foo/uy;
</pre>
<h2 id="相关页面">相关页面</h2>
<ul>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions">正则表达式</a></li>
<li><a href="http://xregexp.com/flags/">XRegEx flags</a> – 正则表达式库提供新的四个标记(<code>n</code>, <code>s</code>, <code>x</code>, <code>A</code>)</li>
</ul>
|