75 lines
3.2 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<h2 id="信息">信息</h2>
<pre><code class="language-javascript">SyntaxError: invalid regular expression flag "x" (Firefox)
SyntaxError: Invalid regular expression flags (Chrome)
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/SyntaxError" title="SyntaxError 对象代表尝试解析语法上不合法的代码的错误。"><code>语法错误</code></a>.</p>
<h2 id="什么地方出错了">什么地方出错了?</h2>
<p>在代码中出现了无效的正则表达式的标记。在一个正则表达式字面量中,由闭合的两条斜线组成一个模式,(正则表达式的)标记定义在第二个(斜线)标记之后。他们也可以通过<a href="Reference/RegExp" title="此页面仍未被本地化, 期待您的翻译!"><code>正则表达式</code></a> 对象的构造函数第二个参数来定义。正则表达式的标记可以单独或者任意次序的组合使用但ECMAScript只规定了五个。</p>
<p>要使正则表达式包含标记,使用此语法:</p>
<pre><code class="language-javascript">var re = /pattern/flags;
</code></pre>
<p></p>
<pre><code class="language-javascript">var re = new RegExp('pattern', 'flags');</code></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 检索将从目标字符串的当前位置开始匹配。参阅<a href="Reference/Global_Objects/RegExp/sticky" title="sticky 属性反映了搜索是否具有粘性 仅从正则表达式的 lastIndex 属性表示的索引处搜索 。sticky 是正则表达式对象的只读属性。"><code>sticky</code></a></td>
</tr>
</tbody>
</table>
<h2 id="示例">示例</h2>
<p>只有5个有效的正则表达式标记。</p>
<pre><code class="language-js example-bad">/foo/bar;
// SyntaxError: invalid regular expression flag "b"
</code></pre>
<p>你打算创建一个正则表达式吗?一个包含两条斜线的表达式被解释为一个正则表达式的字面量。</p>
<pre><code class="language-js example-bad">let obj = {
url: /docs/Web
};
// SyntaxError: invalid regular expression flag "W"
</code></pre>
<p>还是你想创建一个字符串呢?添加单引号或双引号创建一个字符串字面量。</p>
<pre><code class="language-js example-good">let obj = {
url: '/docs/Web'
};</code></pre>
<h3 id="有效的正则表达式标记">有效的正则表达式标记</h3>
<p>在JavaScript中允许的五个有效的正则表达式标记参阅上表。</p>
<pre><code class="language-js example-good">/foo/g;
/foo/gim;
/foo/uy;
</code></pre>
<h2 id="相关页面">相关页面</h2>
<ul>
<li><a href="https://developer.mozilla.orgGuide/Regular_Expressions">正则表达式</a></li>
<li><a class="external" href="http://xregexp.com/flags/" rel="noopener">XRegEx flags</a> 正则表达式库提供新的四个标记(<code>n</code>, <code>s</code>, <code>x</code>, <code>A</code>)</li>
</ul>
</article>