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

100 lines
7.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 class="blockIndicator nonStandard nonStandardHeader">
<p><strong><span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> 非标准</strong><br/>
该特性是非标准的,请尽量不要在生产环境中使用它!</p>
</div></div>
<p>非标准<strong>$1, $2, $3, $4, $5, $6, $7, $8, $9 </strong>属性是包含括号子串匹配的正则表达式的静态和只读属性。</p>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript"><code><var>RegExp</var>.$1
RegExp.$2</code>
RegExp.$3
RegExp.$4
RegExp.$5
RegExp.$6
RegExp.$7
RegExp.$8
RegExp.$9
</code></pre>
<h2 id="描述">描述</h2>
<p>$1, ..., $9 属性是静态的, 他不是独立的的正则表达式属性. 所以, 我们总是像这样子使用他们<code>RegExp.$1</code>, ..., <code>RegExp.$9</code>.</p>
<p>属性的值是只读的而且只有在正确匹配的情况下才会改变.</p>
<p>括号匹配项是无限的, 但是RegExp对象能捕获的只有九个. 你可以通过返回一个数组索引来取得所有的括号匹配项.</p>
<p>这些属性可以在<a href="Reference/Global_Objects/String/replace" title="replace() 方法返回一个由替换值替换一些或所有匹配的模式后的新字符串。模式可以是一个字符串或者一个正则表达式, 替换值可以是一个字符串或者一个每次匹配都要调用的函数。"><code>String.replace</code></a> 方法中替换字符串. 在这种情况下, 不用在前面加上RegExp。下面的例子将详细说明. 当正则表达式中不包含括号, 脚本中的 <code>$n</code>'s 就是字面上的意思 (当n是正整数).</p>
<h2 id="例子">例子</h2>
<h3 id="n_在_String.replace中的应用"><code>$n</code> 在 <code>String.replace中的应用</code></h3>
<p>以下脚本用 <a href="Reference/Global_Objects/String/replace" title="replace() 方法返回一个由替换值替换一些或所有匹配的模式后的新字符串。模式可以是一个字符串或者一个正则表达式, 替换值可以是一个字符串或者一个每次匹配都要调用的函数。"><code>replace()</code></a> 方法去匹配一个first last格式的 name<a href="Reference/String" title="此页面仍未被本地化, 期待您的翻译!"><code>String</code></a> 实例 输出last first格式. 在替换文本里, 脚本用 <code>$1</code> 和 <code>$2</code> 表示正则表达式中的括号匹配项的结果.</p>
<pre><code class="language-javascript">var re = /(\w+)\s(\w+)/;
var str = 'John Smith';
str.replace(re, '$2, $1'); // "Smith, John"
RegExp.$1; // "John"
RegExp.$2; // "Smith"
</code></pre>
<h2 id="技术指标">技术指标</h2>
<p>非标准. 不属于当前的任何规范.</p>
<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>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</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: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="参见">参见</h2>
<ul>
<li><span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> <a href="Reference/Global_Objects/RegExp/input" title="input 非标准属性是正则表达式静态属性含有正则表达式所匹配的字符串。RegExp.$_是这个属性的别名。"><code>RegExp.input ($_)</code></a></li>
<li><span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> <a href="Reference/Global_Objects/RegExp/lastMatch" title="lastMatch 非标准属性是正则表达式的静态和只读属性含有最后匹配到的字符串。RegExp.$&amp; 是这个属性的别名。"><code>RegExp.lastMatch ($&amp;)</code></a></li>
<li><span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> <a href="Reference/Global_Objects/RegExp/lastParen" title="lastParen 非标准属性是正则表达式的静态和只读属性包含匹配到的最后一个子串如果存在。RegExp.$+是这一属性的别名。"><code>RegExp.lastParen ($+)</code></a></li>
<li><span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> <a href="Reference/Global_Objects/RegExp/leftContext" title="leftContext 非标准属性是正则表达式的静态和只读属性,含有最新匹配的左侧子串。 RegExp.$` 是这个属性的别名。"><code>RegExp.leftContext ($`)</code></a></li>
<li><span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> <a href="Reference/Global_Objects/RegExp/rightContext" title="rightContext 非标准属性是正则表达式的静态和只读属性,含有最新匹配的右侧子串。 RegExp.$' 是这个属性的别名。"><code>RegExp.rightContext ($')</code></a></li>
</ul>
</article>