2019-04-21 11:50:48 +08:00

179 lines
8.9 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>
<p><strong><code>sticky</code></strong> 属性反映了搜索是否具有粘性( 仅从正则表达式的 <a href="Reference/Global_Objects/RegExp/lastIndex" title="lastIndex 是正则表达式的一个可读可写的整型属性用来指定下一次匹配的起始索引。"><code>lastIndex</code></a> 属性表示的索引处搜索 )。<code>sticky</code> 是正则表达式对象的只读属性。</p>
<div><table class="standard-table">
<thead>
<tr>
<th class="header" colspan="2"><code>RegExp.prototype.sticky</code> 属性的属性特性:</th>
</tr>
</thead>
<tbody>
<tr>
<td>writable</td>
<td>false</td>
</tr>
<tr>
<td>enumerable</td>
<td>false</td>
</tr>
<tr>
<td>configurable</td>
<td>true</td>
</tr>
</tbody>
</table></div>
<h2 id="描述">描述</h2>
<p><code>sticky</code> 的值是 <a href="Reference/Boolean" title="此页面仍未被本地化, 期待您的翻译!"><code>Boolean</code></a> ,并在“<code>y</code>”标志使用时为真; 否则为假。"<code>y</code>" 标志指示,仅从正则表达式的 <a href="Reference/Global_Objects/RegExp/lastIndex" title="lastIndex 是正则表达式的一个可读可写的整型属性用来指定下一次匹配的起始索引。"><code>lastIndex</code></a> 属性表示的索引处为目标字符串匹配(并且不会尝试从后续索引匹配)。</p>
<p>你不能直接更改这个属性,它是只读的。</p>
<h2 id="例子">例子</h2>
<h3 id="使用带_sticky_标志的正则表达式">使用带 sticky 标志的正则表达式</h3>
<pre><code class="language-javascript">var str = '#foo#';
var regex = /foo/y;
regex.lastIndex = 1;
regex.test(str); // true (译注:此例仅当 lastIndex = 1 时匹配成功,这就是 sticky 的作用)
regex.lastIndex = 5;
regex.test(str); // false lastIndex 被 sticky 标志考虑到,从而导致匹配失败)
regex.lastIndex; // 0 (匹配失败后重置)
</code></pre>
<h3 id="锚定的_sticky_标志">锚定的 sticky 标志</h3>
<p>火狐的 SpiderMonkey 引擎的几个版本有一个 <a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=773687" rel="noopener">bug</a>,处理 <code>^</code> 断言和 sticky 标志时,会允许使用了 sticky 标志的表达式从 <code>^</code> 断言开始匹配,这是不应该的。这个 bug 是在 Firefox 3.6 之后的某个版本引入的which had the sticky flag but not the bug并于2015年修复。 可能正因为这个 bug ES2015 规范 <a class="external" href="http://www.ecma-international.org/ecma-262/7.0/index.html#sec-assertion" rel="noopener">特别指出</a></p>
<blockquote>
<p>当使用带有y标识的匹配模式时^断言总是会匹配输入的开始位置或者(如果是多行模式)每一行的开始位置。</p>
</blockquote>
<p>正确行为的示例:</p>
<pre><code class="language-javascript">var regex = /^foo/y;
regex.lastIndex = 2;
regex.test("..foo"); // false - 索引2不是字符串的开始
var regex2 = /^foo/my;
regex2.lastIndex = 2;
regex2.test("..foo"); // false - 索引2不是字符串或行的开始
regex2.lastIndex = 2;
regex2.test(".\nfoo"); // true - 索引2是行的开始
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-get-regexp.prototype.sticky" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">RegExp.prototype.sticky</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>Initial definition.</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-get-regexp.prototype.sticky" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">RegExp.prototype.sticky</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div></div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Edge</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><a href="/en-US/Firefox/Releases/3" title="Released on 2008-06-17.">3.0</a> (1.9)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
<tr>
<td>Prototype accessor property</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><a href="/en-US/Firefox/Releases/38" title="Released on 2015-05-19.">38</a> (38)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
<tr>
<td>Anchored sticky(y) flag behavior per ES2015</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><a href="/en-US/Firefox/Releases/44" title="Released on 2016-01-26.">44</a> (44)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>1.0 (1.9)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
<tr>
<td>Prototype accessor property</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>38.0 (38)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
<tr>
<td>Anchored sticky(y) flag behavior per ES2015</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>44.0 (44)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/RegExp/lastIndex" title="lastIndex 是正则表达式的一个可读可写的整型属性用来指定下一次匹配的起始索引。"><code>RegExp.lastIndex</code></a></li>
<li><a href="Reference/Global_Objects/RegExp/global" title='global 属性表明正则表达式是否使用了 "g" 标志。global 是一个正则表达式实例的只读属性。'><code>RegExp.prototype.global</code></a></li>
<li><a href="Reference/Global_Objects/RegExp/ignoreCase" title='ignoreCase 属性表明正则表达式是否使用了 "i" 标志。ignoreCase 是正则表达式实例的只读属性。'><code>RegExp.prototype.ignoreCase</code></a></li>
<li><a href="Reference/Global_Objects/RegExp/multiline" title='multiline 属性表明正则表达式是否使用了 "m" 标志。multiline 是正则表达式实例的一个只读属性。'><code>RegExp.prototype.multiline</code></a></li>
<li><a href="Reference/Global_Objects/RegExp/source" title="source 属性返回一个值为当前正则表达式对象的模式文本的字符串,该字符串不会包含正则字面量两边的斜杠以及任何的标志字符。"><code>RegExp.prototype.source</code></a></li>
</ul>
</article>