diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/regexp')
29 files changed, 3390 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/@@match/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/@@match/index.html new file mode 100644 index 0000000000..28b4431659 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/@@match/index.html @@ -0,0 +1,109 @@ +--- +title: 'RegExp.prototype[@@match]()' +slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@match +tags: + - JavaScript + - RegExp + - 正则 +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@match +--- +<div>{{JSRef}}</div> + +<p>对<em>正则表达式</em>匹配<em>字符串</em>时,<strong><code>[@@match]()</code></strong>方法用于获取匹配结果。</p> + +<p>{{EmbedInteractiveExample("pages/js/regexp-prototype-@@match.html")}}</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><var>regexp</var>[Symbol.match](str)</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>str</code></dt> + <dd>match 的目标参数是{{jsxref("String")}}</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>match 方法会返回一个数组,它包括整个匹配结果,和通过捕获组匹配到的结果,如果没有匹配到则返回null</p> + +<h2 id="描述">描述</h2> + +<p>这个方法在 {{jsxref("String.prototype.match()")}} 的内部调用。例如,下面的两个方法返回相同结果。</p> + +<pre class="brush: js">'abc'.match(/a/); + +/a/[Symbol.match]('abc');</pre> + +<p>这个方法为自定义 <code>RegExp</code> 子类中的匹配行为而存在。</p> + +<h2 id="示例">示例</h2> + +<h3 id="直接调用">直接调用</h3> + +<p>这个方法的使用方式和 {{jsxref("String.prototype.match()")}} 相同,不同之处是 <code>this</code> 和参数顺序。</p> + +<pre class="brush: js">var re = /[0-9]+/g; +var str = '2016-01-02'; +var result = re[Symbol.match](str); +console.log(result); // ["2016", "01", "02"] +</pre> + +<h3 id="在子类中使用match">在子类中使用<code>@@match</code></h3> + +<p>{{jsxref("RegExp")}} 的子类可以覆写 <code>[@@match]()</code>方法来修改默认行为。</p> + +<pre class="brush: js">class MyRegExp extends RegExp { + [Symbol.match](str) { + var result = RegExp.prototype[Symbol.match].call(this, str); + if (!result) return null; + return { + group(n) { + return result[n]; + } + }; + } +} + +var re = new MyRegExp('([0-9]+)-([0-9]+)-([0-9]+)'); +var str = '2016-01-02'; +var result = str.match(re); // String.prototype.match calls re[@@match]. +console.log(result.group(1)); // 2016 +console.log(result.group(2)); // 01 +console.log(result.group(3)); // 02 +</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>{{SpecName('ESDraft', '#sec-regexp.prototype-@@match', 'RegExp.prototype[@@match]')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> +<p>{{Compat("javascript.builtins.RegExp.@@match")}}</p> +</div> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{jsxref("String.prototype.match()")}}</li> + <li>{{jsxref("RegExp.prototype.@@replace()", "RegExp.prototype[@@replace]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@search()", "RegExp.prototype[@@search]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@split()", "RegExp.prototype[@@split]()")}}</li> + <li>{{jsxref("RegExp.prototype.exec()")}}</li> + <li>{{jsxref("RegExp.prototype.test()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/@@matchall/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/@@matchall/index.html new file mode 100644 index 0000000000..3cc1465704 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/@@matchall/index.html @@ -0,0 +1,89 @@ +--- +title: 'RegExp.prototype[@@matchAll]()' +slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@matchAll +tags: + - JavaScript + - 正则表达式 +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@matchAll +--- +<p>{{JSRef}}</p> + +<p> <strong><code>[@@matchAll]</code></strong>方法返回对字符串使用正则表达式的所有匹配项。</p> + +<div>\{{EmbedInteractiveExample("pages/js/regexp-prototype-@@matchall.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><var>regexp</var>[Symbol.matchAll](<var>str</var>)</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>str</code></dt> + <dd>一个{{jsxref("String")}}的匹配对象。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>一个<a href="/zh-CN/docs/Web/JavaScript/Guide/Iterators_and_Generators">迭代器</a>。</p> + +<h2 id="描述">描述</h2> + +<p>本方法在{{jsxref("String.prototype.matchAll()")}}中被内部调用。例如,以下两个示例返回相同的结果。</p> + +<pre class="brush: js">'abc'.matchAll(/a/); + +/a/[Symbol.matchAll]('abc');</pre> + +<p>本方法用于自定义<code>RegExp</code>子类中的匹配行为。</p> + +<h2 id="示例">示例</h2> + +<h3 id="直接调用">直接调用</h3> + +<p>本方法的使用方法几乎与{{jsxref("String.prototype.matchAll()")}}相同,除了<code>this</code> 的不同以及参数顺序的的差异。</p> + +<pre class="brush: js">var re = /[0-9]+/g; +var str = '2016-01-02'; +var result = re[Symbol.matchAll](str); + +console.log(<span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="cm-variable">Array</span>.<span class="cm-property">from</span>(<span class="cm-variable">result</span>, <span class="cm-def">x</span> <span class="cm-operator">=></span> <span class="cm-variable-2">x</span>[<span class="cm-number">0</span>])</span></span></span>); +// ["2016", "01", "02"] +</pre> + +<h3 id="在子类中使用matchAll">在子类中使用<code>@@matchAll</code></h3> + +<p>{{jsxref("RegExp")}}的子类可以重写<code>[@@matchAll]()</code>方法来修改默认行为。例如,返回一个{{jsxref("Array")}}而不是<a href="/zh-CN/docs/Web/JavaScript/Guide/Iterators_and_Generators">iterator</a>:</p> + +<pre class="brush: js">class MyRegExp extends RegExp { + [Symbol.matchAll](str) { + var result = RegExp.prototype[Symbol.matchAll].call(this, str); + if (!result) { + return null; + } else { + return Array.from(result); + } + } +} + +var re = new MyRegExp('([0-9]+)-([0-9]+)-([0-9]+)', 'g'); +var str = '2016-01-02|2019-03-07'; +var result = str.matchAll(re); +console.log(result[0]); // [ "2016-01-02", "2016", "01", "02" ] +console.log(result[1]); // [ "2019-03-07", "2019", "03", "07" ] +</pre> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div> + +<p>{{Compat("javascript.builtins.RegExp.@@matchAll")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{JSxRef("String.prototype.matchAll()")}}</li> + <li>{{JSxRef("Symbol.matchAll")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/@@replace/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/@@replace/index.html new file mode 100644 index 0000000000..a8604f9415 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/@@replace/index.html @@ -0,0 +1,120 @@ +--- +title: 'RegExp.prototype[@@replace]()' +slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@replace +tags: + - JavaScript + - RegExp + - 正则表达式 +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@replace +--- +<div>{{JSRef}}</div> + +<p> <strong><code>[@@replace]()</code></strong> 方法会在一个字符串中用给定的替换器,替换所有符合正则模式的匹配项,并返回替换后的新字符串结果。用来替换的参数可以是一个字符串或是一个针对每次匹配的回调函数。</p> + +<p>{{EmbedInteractiveExample("pages/js/regexp-prototype-@@replace.html")}}</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><var>regexp</var>[Symbol.replace](str, <var>newSubStr</var>|<var>function</var>)</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>str</code></dt> + <dd>正则替换的目标字符串。</dd> + <dt><code>newSubStr (replacement)</code></dt> + <dd>类型为 {{jsxref("String")}} 的替换器。支持大多数特殊的替换匹配模式; 见{{jsxref("String.prototype.replace()")}}页的{{jsxref("String.prototype.replace", "Specifying a string as a parameter", "#Specifying_a_string_as_a_parameter", 1)}}部分。</dd> + <dt><code>function (replacement)</code></dt> + <dd>生成新的子字符串的回调函数替换器。作用于该函数的参数的详细描述见{{jsxref("String.prototype.replace()")}}页的 {{jsxref("String.prototype.replace", "Specifying a function as a parameter", "#Specifying_a_function_as_a_parameter", 1)}} 部分。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>用替换器替换相应匹配项后的新字符串。</p> + +<h2 id="描述">描述</h2> + +<p>如果匹配模式也是{{jsxref("RegExp")}}对象,这个方法在 {{jsxref("String.prototype.replace()")}} 的内部调用。例如,下面的两个方法返回相同结果。</p> + +<pre class="brush: js">'abc'.replace(/a/, 'A'); + +/a/[Symbol.replace]('abc', 'A');</pre> + +<p>该方法是为了在RegExp子类中自定义匹配的替换模式。</p> + +<p>如果匹配模式不是一个{{jsxref("RegExp")}} 对象, {{jsxref("String.prototype.replace()")}} 就不会调用该方法,也不会创建一个 {{jsxref("RegExp")}}对象。</p> + +<h2 id="示例">示例</h2> + +<h3 id="直接调用">直接调用</h3> + +<p>这个方法基本可以和 {{jsxref("String.prototype.replace()")}} 一样使用, 不同之处是 <code>this</code> 和参数顺序。</p> + +<pre class="brush: js">var re = /-/g; +var str = '2016-01-01'; +var newstr = re[Symbol.replace](str, '.'); +console.log(newstr); // 2016.01.01 +</pre> + +<h3 id="在子类中使用replace">在子类中使用@@replace</h3> + +<p>{{jsxref("RegExp")}} 的子类可以覆写 <code>[@@replace]()</code>方法来修改默认行为。</p> + +<pre class="brush: js">class MyRegExp extends RegExp { + constructor(pattern, flags, count) { + super(pattern, flags); + this.count = count; + } + [Symbol.replace](str, replacement) { + // Perform @@replace |count| times. + var result = str; + for (var i = 0; i < this.count; i++) { + result = RegExp.prototype[Symbol.replace].call(this, result, replacement); + } + return result; + } +} + +var re = new MyRegExp('\\d', '', 3); +var str = '01234567'; +var newstr = str.replace(re, '#'); // String.prototype.replace calls re[@@replace]. +console.log(newstr); // ###34567</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">备注</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-regexp.prototype-@@replace', 'RegExp.prototype[@@replace]')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>初始定义</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp.prototype-@@replace', 'RegExp.prototype[@@replace]')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> +<p>{{Compat("javascript.builtins.RegExp.@@replace")}}</p> +</div> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{jsxref("String.prototype.replace()")}}</li> + <li>{{jsxref("RegExp.prototype.@@match()", "RegExp.prototype[@@match]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@search()", "RegExp.prototype[@@search]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@split()", "RegExp.prototype[@@split]()")}}</li> + <li>{{jsxref("RegExp.prototype.exec()")}}</li> + <li>{{jsxref("RegExp.prototype.test()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/@@search/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/@@search/index.html new file mode 100644 index 0000000000..c88398e1a2 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/@@search/index.html @@ -0,0 +1,153 @@ +--- +title: 'RegExp.prototype[@@search]()' +slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@search +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@search +--- +<div>{{JSRef}}</div> + +<p><strong><code>[@@search]()</code></strong> 方法执行了一个在给定字符串中的一个搜索以取得匹配正则模式的项。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><var>regexp</var>[Symbol.search](str)</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>str</code></dt> + <dd>搜索的目标 {{jsxref("String")}}。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<dl> + <dt>整数</dt> + <dd>如果成功的话,<code>[@@search]()</code> 返回该正则模式的第一个匹配项的在字符串中的位置索引。否则将返回-1。</dd> +</dl> + +<h2 id="描述">描述</h2> + +<p>这个方法在 {{jsxref("String.prototype.search()")}} 的内部调用。例如,下面的两个方法返回相同结果。</p> + +<pre class="brush: js">'abc'.search(/a/); + +/a/[Symbol.search]('abc');</pre> + +<p>这个方法为自定义 <code>RegExp</code> 子类中的匹配行为而存在。</p> + +<h2 id="示例">示例</h2> + +<h3 id="直接调用">直接调用</h3> + +<p>这个方法的使用方式和 {{jsxref("String.prototype.search()")}} 相同,不同之处是 <code>this</code> 和参数顺序。</p> + +<pre class="brush: js">var re = /-/g; +var str = '2016-01-02'; +var result = re[Symbol.search](str); +console.log(result); // 4 +</pre> + +<h3 id="在子类中使用search">在子类中使用<code>@@search</code></h3> + +<p>{jsxref("RegExp")}} 的子类可以覆写 <code>[@@search]()</code>方法来修改默认行为。</p> + +<pre class="brush: js">class MyRegExp extends RegExp { + constructor(str) { + super(str) + this.pattern = str; + } + [Symbol.search](str) { + return str.indexOf(this.pattern); + } +} + +var re = new MyRegExp('a+b'); +var str = 'ab a+b'; +var result = str.search(re); // String.prototype.search calls re[@@search]. +console.log(result); // 3 +</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>{{SpecName('ES6', '#sec-regexp.prototype-@@search', 'RegExp.prototype[@@search]')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>初始定义</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp.prototype-@@search', 'RegExp.prototype[@@search]')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</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>{{CompatUnknown}}</td> + <td>{{CompatGeckoDesktop(49)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</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>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile(49)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{jsxref("String.prototype.search()")}}</li> + <li>{{jsxref("RegExp.prototype.@@match()", "RegExp.prototype[@@match]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@replace()", "RegExp.prototype[@@replace]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@split()", "RegExp.prototype[@@split]()")}}</li> + <li>{{jsxref("RegExp.prototype.exec()")}}</li> + <li>{{jsxref("RegExp.prototype.test()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/@@species/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/@@species/index.html new file mode 100644 index 0000000000..49f72f06f7 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/@@species/index.html @@ -0,0 +1,111 @@ +--- +title: 'get RegExp[@@species]' +slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@species +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@species +--- +<div>{{JSRef}}</div> + +<p><code><strong>RegExp[@@species]</strong></code> 访问器属性返回<code>RegExp</code> 的构造器。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">RegExp[Symbol.species] +</pre> + +<h2 id="描述">描述</h2> + +<p><code>species</code> 访问器属性返回 <code>RegExp</code> 对象的默认构造器。子类构造器可能会覆盖它,来修改构造器的指派。</p> + +<h2 id="示例">示例</h2> + +<p><code>species</code>属性返回默认构造器函数,它是用于<code>RegExp</code> 对象的<code>RegExp</code>构造器:</p> + +<pre class="brush: js">RegExp[Symbol.species]; // 函数 RegExp()</pre> + +<p>在派生的正则类(也就是你自定义的正则类 <code>MyRegExp</code>)中,<code>MyRegExp</code> 的 species 是 <code>MyRegExp</code> 构造器。但是,你可能希望覆盖它,以便在你的派生类方法中,返回 <code>RegExp</code> 父类对象:</p> + +<pre class="brush: js">class MyRegExp extends RegExp { + // 将 MyRegExp species 覆盖为 RegExp 父类构造器 + static get [Symbol.species]() { return RegExp; } +}</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>{{SpecName('ES6', '#sec-get-regexp-@@species', 'get RegExp [ @@species ]')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>初始定义。</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-get-regexp-@@species', 'get RegExp [ @@species ]')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</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>{{CompatUnknown}}</td> + <td>{{CompatGeckoDesktop("49")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</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>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile("49")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{jsxref("RegExp")}}</li> + <li>{{jsxref("Symbol.species")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/@@split/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/@@split/index.html new file mode 100644 index 0000000000..b3bfa32317 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/@@split/index.html @@ -0,0 +1,151 @@ +--- +title: 'RegExp.prototype[@@split]()' +slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@split +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@split +--- +<div>{{JSRef}}</div> + +<p><strong><code>[@@split]()</code></strong> 方法切割 {{jsxref("String")}} 对象为一个其子字符串的数组 。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><var>regexp</var>[Symbol.split](str[, <var>limit</var>])</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>str</code></dt> + <dd>切割操作的目标字符串</dd> + <dt><code>limit</code></dt> + <dd> + <p>可选。一个为了限制切割数量的特定整数。 <code>[@@split]()</code> 防范仍会切割每个匹配正则模式的匹配项,直到切割数量达到该限制数,除非提前切割完字符串。</p> + </dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>包含其子字符串的{{jsxref("Array")}} 。</p> + +<h2 id="描述">描述</h2> + +<p>如果切割器是一个{{jsxref("RegExp")}}对象,这个方法就将在 {{jsxref("String.prototype.split()")}} 的内部调用。例如,下面的两个方法返回相同结果。</p> + +<pre class="brush: js">'a-b-c'.split(/-/); + +/-/[Symbol.split]('a-b-c');</pre> + +<p>这个方法为自定义 <code>RegExp</code> 子类中的匹配行为而存在。</p> + +<p>如果str参数不是一个{{jsxref("RegExp")}} 对象, {{jsxref("String.prototype.split()")}} 就不会调用该方法,也不会创建一个 {{jsxref("RegExp")}} 对象。示例</p> + +<h3 id="直接调用">直接调用</h3> + +<p>这个方法的使用方式和 {{jsxref("String.prototype.split()")}} 相同,不同之处是 <code>this</code> 和参数顺序。</p> + +<pre class="brush: js">var re = /-/g; +var str = '2016-01-02'; +var result = re[Symbol.split](str); +console.log(result); // ["2016", "01", "02"] +</pre> + +<h3 id="在子类中使用_split">在子类中使用 <code>@@split</code></h3> + +<p>{{jsxref("RegExp")}} 的子类可以覆写 <code>[@@split]()</code>方法来修改默认行为。</p> + +<pre class="brush: js">class MyRegExp extends RegExp { + [Symbol.split](str, limit) { + var result = RegExp.prototype[Symbol.split].call(this, str, limit); + return result.map(x => "(" + x + ")"); + } +} + +var re = new MyRegExp('-'); +var str = '2016-01-02'; +var result = str.split(re); // String.prototype.split calls re[@@split]. +console.log(result); // ["(2016)", "(01)", "(02)"] +</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>{{SpecName('ES6', '#sec-regexp.prototype-@@split', 'RegExp.prototype[@@split]')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>初始定义</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp.prototype-@@split', 'RegExp.prototype[@@split]')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</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>{{CompatUnknown}}</td> + <td>{{CompatGeckoDesktop(49)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</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>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile(49)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{jsxref("String.prototype.split()")}}</li> + <li>{{jsxref("RegExp.prototype.@@match()", "RegExp.prototype[@@match]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@replace()", "RegExp.prototype[@@replace]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@search()", "RegExp.prototype[@@search]()")}}</li> + <li>{{jsxref("RegExp.prototype.exec()")}}</li> + <li>{{jsxref("RegExp.prototype.test()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/compile/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/compile/index.html new file mode 100644 index 0000000000..e5b080a839 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/compile/index.html @@ -0,0 +1,131 @@ +--- +title: RegExp.prototype.compile() +slug: Web/JavaScript/Reference/Global_Objects/RegExp/compile +tags: + - Deprecated + - JavaScript +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/compile +--- +<div>{{JSRef}} {{deprecated_header}}</div> + +<p><code>已废弃的<strong>compile</strong></code><strong><code>()</code></strong> 方法被用于在脚本执行过程中(重新)编译正则表达式。与<code>RegExp</code>构造函数基本一样。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><var>regexObj</var>.compile(<var>pattern, flags</var>)</code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>pattern</code></dt> + <dd>正则表达式的文本 。</dd> + <dt><code>flags</code></dt> + <dd> + <p>如果指定,标志可以具有以下值的任意组合:</p> + + <dl> + <dt><code>g</code></dt> + <dd>全局匹配</dd> + <dt><code>i</code></dt> + <dd>忽略大小写</dd> + <dt><code>m</code></dt> + <dd>多行;让开始和结束字符(^ 和 $)工作在多行模式工作(例如,^ 和 $ 可以匹配字符串中每一行的开始和结束(行是由 \n 或 \r 分割的),而不只是整个输入字符串的最开始和最末尾处。</dd> + <dt><code>y</code></dt> + <dd>黏度; 在目标字符串中,只从正则表达式的lastIndex属性指定的显示位置开始匹配(并且不试图从任何之后的索引匹配)。</dd> + </dl> + </dd> +</dl> + +<h2 id="描述">描述</h2> + +<p><code>不推荐compile方法。你可以使用</code> <code>RegExp</code> 构造函数来得到相同效果。</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用compile()"><code>使用compile()</code></h3> + +<p>以下展示如何用新模式和新标志来重新编译正则表达式。</p> + +<pre class="brush: js">var regexObj = new RegExp("foo", "gi"); +regexObj.compile("new foo", "g"); +</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>{{SpecName('ES6', '#sec-regexp.prototype.compile', 'RegExp.prototype.compile')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition. Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp.prototype.compile', 'RegExp.prototype.compile')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td>Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("RegExp")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/dotall/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/dotall/index.html new file mode 100644 index 0000000000..8608cfd1c2 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/dotall/index.html @@ -0,0 +1,47 @@ +--- +title: RegExp.prototype.dotAll +slug: Web/JavaScript/Reference/Global_Objects/RegExp/dotAll +tags: + - JavaScript + - 修饰符 + - 正则表达式 +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/dotAll +--- +<p>{{JSRef}}{{Draft}}</p> + +<p><strong><code>dotAll</code></strong> 属性表明是否在正则表达式中一起使用"<code>s</code>"修饰符(引入/s修饰符,使得.可以匹配任意单个字符)。<code>dotAll</code> 是一个只读的属性,属于单个正则表达式实例。</p> + +<p>{{JS_Property_Attributes(0, 0, 1)}}</p> + +<h2 id="描述">描述</h2> + +<p>如果使用了"<code>s</code>"修饰符,<code>dotAll</code> 的值将返回{{JSxRef("Boolean")}}类型的<code>true</code>,否则将返回<code>false</code>。"<code>s</code>"修饰符表示,特殊字符"<code>.</code>"应另外匹配字符串中的下述行终结符(line terminator characters),否则将会失配:</p> + +<ul> + <li>U+000A 换行符("<code>\n</code>")</li> + <li>U+000D 回车符("<code>\r</code>")</li> + <li>U+2028 行分隔符(<span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="objectBox objectBox-string">line separator</span></span></span></span>)</li> + <li>U+2029 段分隔符(<span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="objectBox objectBox-string">paragraph separator</span></span></span></span>)</li> +</ul> + +<p>这实际上意味着"<code>.</code>"将会匹配任意的单个Unicode Basic Multilingual Plane (BMP)字符。若要使其与astral字符(大于\uFFFF的Unicode字符)匹配,你应当使用"<code>u</code>"(Unicode)修饰符。一起使用这两个修饰符,"<code>.</code>"将无一例外地匹配任意Unicode字符。</p> + +<p>无法直接修改此属性。</p> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div> + +<p>{{Compat("javascript.builtins.RegExp.dotAll")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{JSxRef("RegExp.lastIndex")}}</li> + <li>{{JSxRef("RegExp.prototype.global")}}</li> + <li>{{JSxRef("RegExp.prototype.ignoreCase")}}</li> + <li>{{JSxRef("RegExp.prototype.multiline")}}</li> + <li>{{JSxRef("RegExp.prototype.source")}}</li> + <li>{{JSxRef("RegExp.prototype.sticky")}}</li> + <li>{{JSxRef("RegExp.prototype.unicode")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/exec/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/exec/index.html new file mode 100644 index 0000000000..6648813f00 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/exec/index.html @@ -0,0 +1,199 @@ +--- +title: RegExp.prototype.exec() +slug: Web/JavaScript/Reference/Global_Objects/RegExp/exec +tags: + - '## lastIndex bug???' + - JavaScript + - Method + - Prototype + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/exec +--- +<div>{{JSRef}}</div> + +<p><code><strong>exec() </strong></code>方法在一个指定字符串中执行一个搜索匹配。返回一个结果数组或 {{jsxref("null")}}。</p> + +<p>在设置了 {{jsxref("RegExp.global", "global")}} 或 {{jsxref("RegExp.sticky", "sticky")}} 标志位的情况下(如 <code>/foo/g</code> or <code>/foo/y</code>),JavaScript {{jsxref("RegExp")}} 对象是<strong>有状态</strong>的。他们会将上次成功匹配后的位置记录在 <span style="font-size: 1rem; letter-spacing: -0.00278rem;">{{jsxref("RegExp.lastIndex", "lastIndex")}} 属性中。使用此特性,</span><code style="font-size: 1rem; letter-spacing: -0.00278rem;">exec()</code><span style="font-size: 1rem; letter-spacing: -0.00278rem;"> 可用来对单个字符串中的多次匹配结果进行逐条的遍历(包括捕获到的匹配),而相比之下, {{jsxref("String.prototype.match()")}} 只会返回匹配到的结果。</span></p> + +<p>如果你只是为了判断是否匹配(true或 false),可以使用 {{jsxref("RegExp.test()")}} 方法,或者 {{jsxref("String.search()")}} 方法。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-exec.html")}}</div> + + + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><var>regexObj</var>.exec(<var>str</var>)</pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<dl> + <dt><code>str</code></dt> + <dd>要匹配正则表达式的字符串。</dd> +</dl> + +<h3 id="Description" name="Description">返回值</h3> + +<p>如果匹配成功,<code>exec()</code> 方法返回一个数组(包含额外的属性 <code>index</code> 和 <code>input</code> ,参见下方表格),并更新正则表达式对象的 {{jsxref("RegExp.lastIndex", "lastIndex")}} 属性。完全匹配成功的文本将作为返回数组的第一项,从第二项起,后续每项都对应正则表达式内捕获括号里匹配成功的文本。</p> + +<p>如果匹配失败,<code>exec()</code> 方法返回 {{jsxref("null")}},并将 {{jsxref("RegExp.lastIndex", "lastIndex")}} 重置为 0 。</p> + +<h2 id="描述">描述</h2> + +<p>考虑以下示例:</p> + +<pre class="brush: js">// Match "quick brown" followed by "jumps", ignoring characters in between +// Remember "brown" and "jumps" +// Ignore case +var re = /quick\s(brown).+?(jumps)/ig; +var result = re.exec('The Quick Brown Fox Jumps Over The Lazy Dog'); +</pre> + +<p>下表列出这个脚本的返回值:</p> + +<table class="fullwidth-table"> + <tbody> + <tr> + <td class="header">对象</td> + <td class="header">属性/索引</td> + <td class="header">描述</td> + <td class="header">例子</td> + </tr> + <tr> + <td rowspan="4"><code>result</code></td> + <td><code>[0]</code></td> + <td>匹配的全部字符串</td> + <td><code>Quick Brown Fox Jumps</code></td> + </tr> + <tr> + <td><code>[1], ...[<em>n</em> ]</code></td> + <td>括号中的分组捕获</td> + <td><code>[1] = Brown<br> + [2] = Jumps</code></td> + </tr> + <tr> + <td><code>index</code></td> + <td>匹配到的字符位于原始字符串的基于0的索引值</td> + <td><code>4</code></td> + </tr> + <tr> + <td><code>input</code></td> + <td>原始字符串</td> + <td><code>The Quick Brown Fox Jumps Over The Lazy Dog</code></td> + </tr> + <tr> + <td rowspan="5"><code>re</code></td> + <td><code>lastIndex</code></td> + <td>下一次匹配开始的位置</td> + <td><code>25</code></td> + </tr> + <tr> + <td><code>ignoreCase</code></td> + <td>是否使用了 "<code>i</code>" 标记使正则匹配忽略大小写</td> + <td><code>true</code></td> + </tr> + <tr> + <td><code>global</code></td> + <td>是否使用了 "<code>g</code>" 标记来进行全局的匹配.</td> + <td><code>true</code></td> + </tr> + <tr> + <td><code>multiline</code></td> + <td> + <p>是否使用了 "<code>m</code>" 标记使正则工作在多行模式(也就是,^ 和 $ 可以匹配字符串中每一行的开始和结束(行是由 \n 或 \r 分割的),而不只是整个输入字符串的最开始和最末尾处。)</p> + </td> + <td><code>false</code></td> + </tr> + <tr> + <td><code>source</code></td> + <td>正则匹配的字符串</td> + <td><code>quick\s(brown).+?(jumps)</code></td> + </tr> + </tbody> +</table> + +<h2 id="示例">示例</h2> + +<h3 id="查找所有匹配">查找所有匹配</h3> + +<p>当正则表达式使用 "<code>g</code>" 标志时,可以多次执行 <code>exec</code> 方法来查找同一个字符串中的成功匹配。当你这样做时,查找将从正则表达式的 {{jsxref("RegExp.lastIndex", "lastIndex")}} 属性指定的位置开始。({{jsxref("RegExp.test", "test()")}} 也会更新 <code>lastIndex</code> 属性)。注意,即使再次查找的字符串不是原查找字符串时,{{jsxref("RegExp.lastIndex", "lastIndex")}} 也不会被重置,它依旧会从记录的 {{jsxref("RegExp.lastIndex", "lastIndex")}} 开始。</p> + +<p>例如,你使用下面的脚本:</p> + +<pre class="brush: js">var myRe = /ab*/g; +var str = 'abbcdefabh'; +var myArray; +while ((myArray = myRe.exec(str)) !== null) { + var msg = 'Found ' + myArray[0] + '. '; + msg += 'Next match starts at ' + myRe.lastIndex; + console.log(msg); +} +</pre> + +<p>脚本运行结果如下:</p> + +<pre>Found abb. Next match starts at 3 +Found ab. Next match starts at 9 +</pre> + +<div class="blockIndicator warning"> +<p>注意:不要把正则表达式字面量(或者{{jsxref("RegExp")}}构造器)放在 <code>while</code> 条件表达式里。由于每次迭代时 {{jsxref("RegExp.lastIndex", "lastIndex")}} 的属性都被重置,如果匹配,将会造成一个死循环。并且要确保使用了'g'标记来进行全局的匹配,否则同样会造成死循环。</p> +</div> + +<h3 id="结合_RegExp_字面量使用_exec">结合 <code>RegExp</code> 字面量使用 <code>exec()</code></h3> + +<p>你也可以直接使用 <code>exec()</code> 而不是创建一个 {{jsxref("RegExp")}} 对象:</p> + +<pre class="brush: js">var matches = /(hello \S+)/.exec('This is a hello world!'); +console.log(matches[1]); +</pre> + +<p>运行上面的代码,控制台会输出"hello world!" 字符串。</p> + +<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>{{SpecName('ES3')}}</td> + <td>{{Spec2('ES3')}}</td> + <td>Initial definition. Implemented in JavaScript 1.2.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.10.6.21', 'RegExp.exec')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-regexp.prototype.exec', 'RegExp.exec')}}</td> + <td>{{Spec2('ES6')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp.prototype.exec', 'RegExp.exec')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("javascript.builtins.RegExp.exec")}}</p> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions">Regular Expressions</a> chapter in the <a href="/en-US/docs/Web/JavaScript/Guide">JavaScript Guide</a></li> + <li>{{jsxref("RegExp")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/flags/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/flags/index.html new file mode 100644 index 0000000000..bb74a28131 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/flags/index.html @@ -0,0 +1,114 @@ +--- +title: RegExp.prototype.flags +slug: Web/JavaScript/Reference/Global_Objects/RegExp/flags +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/flags +--- +<div>{{JSRef}}</div> + +<p><strong><code>flags</code></strong>属性返回一个字符串,由当前正则表达式对象的标志组成。</p> + +<div>{{js_property_attributes(0, 0, 1)}}</div> + +<h2 id="描述">描述</h2> + +<p><code>flags</code>属性中的标志以字典序排序(从左到右,即<code>"gimuy"</code>)。</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用flags">使用<code>flags</code></h3> + +<pre class="brush: js">/foo/ig.flags; // "gi" +/bar/myu.flags; // "muy" +</pre> + +<h2 id="Polyfill">Polyfill</h2> + +<pre class="brush: js">if (RegExp.prototype.flags === undefined) { + Object.defineProperty(RegExp.prototype, 'flags', { + configurable: true, + get: function() { + return this.toString().match(/[gimuy]*$/)[0]; + } + }); +} +</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>{{SpecName('ES2015', '#sec-get-regexp.prototype.flags', 'RegExp.prototype.flags')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>初始定义。</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-get-regexp.prototype.flags', 'RegExp.prototype.flags')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("37")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</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>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile("37")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{jsxref("RegExp.prototype.source")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/global/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/global/index.html new file mode 100644 index 0000000000..cddfc3724f --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/global/index.html @@ -0,0 +1,107 @@ +--- +title: RegExp.prototype.global +slug: Web/JavaScript/Reference/Global_Objects/RegExp/global +tags: + - JavaScript + - Property + - Prototype + - RegExp +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/global +--- +<div> + {{JSRef("Global_Objects", "RegExp")}}</div> +<h2 id="Summary" name="Summary">概述</h2> +<p><code><strong>global</strong></code> 属性表明正则表达式是否使用了 "<code>g</code>" 标志。<code>global</code> 是一个正则表达式实例的只读属性。</p> +<div> + {{js_property_attributes(0,0,0)}}</div> +<h2 id="Description" name="Description">描述</h2> +<p><code>global</code> 的值是布尔对象,如果使用了 "<code>g</code>" 标志,则返回 <code>true</code>;否则返回 <code>false</code>。 "<code>g</code>" 标志意味着正则表达式应该测试字符串中所有可能的匹配。</p> +<p>你无法直接更改此属性。</p> +<h2 id="Examples" name="Examples">示例</h2> +<h3 id="Example:_Using_MIN_VALUE" name="Example:_Using_MIN_VALUE">例子:使用 <code>global</code></h3> +<pre class="brush:js">var regex = new RegExp("foo", "g") + +console.log(regex.global) // true +</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>ECMAScript 3rd Edition. Implemented in JavaScript 1.2.</td> + <td>Standard</td> + <td>Initial definition.<br> + JavaScript 1.5: <code>global</code> is a property of a <code>RegExp</code> instance, not the <code>RegExp</code> object.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.10.7.2', 'RegExp.prototype.global')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-get-regexp.prototype.global', 'RegExp.prototype.global')}}</td> + <td>{{Spec2('ES6')}}</td> + <td><code>global</code> is now a prototype accessor property rather than an instance's own data property.</td> + </tr> + </tbody> +</table> +<h2 id="浏览器兼容性">浏览器兼容性</h2> +<p>{{ CompatibilityTable() }}</p> +<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>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</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>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<h2 id="See_also" name="See_also">相关链接</h2> +<ul> + <li>{{jsxref("RegExp.prototype.ignoreCase")}}</li> + <li>{{jsxref("RegExp.prototype.lastIndex")}}</li> + <li>{{jsxref("RegExp.prototype.multiline")}}</li> + <li>{{jsxref("RegExp.prototype.source")}}</li> + <li>{{jsxref("RegExp.prototype.sticky")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/ignorecase/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/ignorecase/index.html new file mode 100644 index 0000000000..b2d0742626 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/ignorecase/index.html @@ -0,0 +1,107 @@ +--- +title: RegExp.prototype.ignoreCase +slug: Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase +tags: + - JavaScript + - Property + - Prototype + - RegExp +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase +--- +<div> + {{JSRef("Global_Objects", "RegExp")}}</div> +<h2 id="Summary" name="Summary">概述</h2> +<p><code><strong>ignoreCase</strong></code> 属性表明正则表达式是否使用了 "<code>i</code>" 标志。<code>ignoreCase</code> 是正则表达式实例的只读属性。</p> +<div> + {{js_property_attributes(0,0,0)}}</div> +<h2 id="Description" name="Description">描述</h2> +<p><code>ignoreCase</code> 的值是布尔对象,如果使用了"<code>i</code>" 标志,则返回 <code>true</code>;否则,返回 <code>false</code>。"<code>i</code>" 标志意味着在字符串进行匹配时,应该忽略大小写。</p> +<p>你无法直接更改此属性。</p> +<h2 id="Examples" name="Examples">示例</h2> +<h3 id="Example:_Using_MIN_VALUE" name="Example:_Using_MIN_VALUE">例子:使用 <code>ignoreCase</code></h3> +<pre class="brush:js">var regex = new RegExp("foo", "i") + +console.log(regex.ignoreCase) // true +</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>ECMAScript 3rd Edition. Implemented in JavaScript 1.2.</td> + <td>Standard</td> + <td>Initial definition.<br> + JavaScript 1.5: <code>ignoreCase</code> is a property of a <code>RegExp</code> instance, not the <code>RegExp</code> object.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.10.7.3', 'RegExp.prototype.ignoreCase')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-get-regexp.prototype.ignorecase', 'RegExp.prototype.ignoreCase')}}</td> + <td>{{Spec2('ES6')}}</td> + <td><code>ignoreCase</code> is now a prototype accessor property rather than an instance's own data property.</td> + </tr> + </tbody> +</table> +<h2 id="浏览器兼容性">浏览器兼容性</h2> +<p>{{ CompatibilityTable() }}</p> +<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>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</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>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<h2 id="See_also" name="See_also">相关链接</h2> +<ul> + <li>{{jsxref("RegExp.prototype.global")}}</li> + <li>{{jsxref("RegExp.prototype.lastIndex")}}</li> + <li>{{jsxref("RegExp.prototype.multiline")}}</li> + <li>{{jsxref("RegExp.prototype.source")}}</li> + <li>{{jsxref("RegExp.prototype.sticky")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/index.html new file mode 100644 index 0000000000..77e10c7396 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/index.html @@ -0,0 +1,269 @@ +--- +title: RegExp +slug: Web/JavaScript/Reference/Global_Objects/RegExp +tags: + - Class + - JavaScript + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp +--- +<div>{{JSRef}}</div> + +<p><strong><code>RegExp</code></strong> 对象用于将文本与一个模式匹配。</p> + +<p>有关正则表达式的介绍,请阅读 <a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/">JavaScript指南</a>中的<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions">正则表达式章节</a>。</p> + +<h2 id="描述">描述</h2> + +<h3 id="字面量和构造函数">字面量和构造函数</h3> + +<p>有两种方法可以创建一个 <code>RegExp</code> 对象:一种是字面量,另一种是构造函数。</p> + +<dl> + <dt>字面量</dt> + <dd>由斜杠包围而不是引号包围。</dd> + <dt>构造函数的字符串参数</dt> + <dd>由引号而不是斜杠包围。</dd> +</dl> + +<p>以下三种表达式都会创建相同的正则表达式:</p> + +<pre class="brush: js notranslate" style="font-size: 14px;">/ab+c/i; +new RegExp('ab+c', 'i'); +new RegExp(/ab+c/, 'i');</pre> + +<p>当表达式被赋值时,字面量形式提供正则表达式的编译(compilation)状态,当正则表达式保持为常量时使用字面量。例如当你在循环中使用字面量构造一个正则表达式时,正则表达式不会在每一次迭代中都被重新编译(recompiled)。</p> + +<p>而正则表达式对象的构造函数,如 <code>new RegExp('ab+c')</code> 提供了正则表达式运行时编译(runtime compilation)。如果你知道正则表达式模式将会改变,或者你事先不知道什么模式,而是从另一个来源获取,如用户输入,这些情况都可以使用构造函数。</p> + +<h3 id="构造函数中的标志参数flags">构造函数中的标志参数(flags)</h3> + +<p>从 ECMAScript 6 开始,当第一个参数为正则表达式而第二个标志参数存在时,<code>new RegExp(/ab+c/, 'i')</code> 不再抛出 {{jsxref("TypeError")}} (<code>"从另一个RegExp构造一个RegExp时无法提供标志"</code>)的异常,取而代之,将使用这些参数创建一个新的正则表达式。</p> + +<p>当使用构造函数创造正则对象时,需要常规的字符转义规则(在前面加反斜杠 <code>\</code>)。</p> + +<p>比如,以下是等价的:</p> + +<pre class="brush: js notranslate">var re = new RegExp("\\w+"); +var re = /\w+/;</pre> + +<h3 id="Perl-like_RegExp_属性">Perl-like RegExp 属性</h3> + +<p>请注意,{{jsxref("RegExp")}}属性有长名称和短名称(类似Perl)。两个名称总是引用同一个值。(Perl是JavaScript为其正则表达式建模的编程语言)。另请参见<a href="/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#RegExp_Properties">不推荐使用的RegExp属性。</a></p> + +<h2 id="构造函数">构造函数</h2> + +<dl> + <dt>{{jsxref("RegExp/RegExp", "RegExp()")}}</dt> + <dd>创建一个新的 <code>RegExp</code> 对象。</dd> +</dl> + +<h2 id="静态属性">静态属性</h2> + +<dl> + <dt>{{jsxref("RegExp.@@species", "get RegExp[@@species]")}}</dt> + <dd>该构造函数用于创建派生对象。</dd> + <dt>{{jsxref("RegExp.lastIndex")}}</dt> + <dd>该索引表示从哪里开始下一个匹配</dd> +</dl> + +<h2 id="实例属性">实例属性</h2> + +<dl> + <dt>{{JSxRef("RegExp.prototype.flags")}}</dt> + <dd>含有 <code>RegExp</code> 对象 flags 的字符串。</dd> + <dt>{{JSxRef("RegExp.prototype.dotAll")}}</dt> + <dd><code>.</code> 是否要匹配新行(newlines)。</dd> + <dt>{{JSxRef("RegExp.prototype.global")}}</dt> + <dd>针对字符串中所有可能的匹配项测试正则表达式,还是仅针对第一个匹配项。</dd> + <dt></dt> + <dt></dt> + <dt>{{JSxRef("RegExp.prototype.ignoreCase")}}</dt> + <dd>匹配文本的时候是否忽略大小写。</dd> + <dt>{{JSxRef("RegExp.prototype.multiline")}}</dt> + <dd>是否进行多行搜索。</dd> + <dt>{{JSxRef("RegExp.prototype.source")}}</dt> + <dd>正则表达式的文本。</dd> + <dt>{{JSxRef("RegExp.prototype.sticky")}}</dt> + <dd>搜索是否是 sticky。</dd> + <dt>{{JSxRef("RegExp.prototype.unicode")}}</dt> + <dd>Unicode 功能是否开启。</dd> +</dl> + +<h2 id="实例方法">实例方法</h2> + +<dl> + <dt>{{JSxRef("RegExp.prototype.compile()")}}</dt> + <dd>运行脚本的期间(重新)编译正则表达式。</dd> + <dt>{{JSxRef("RegExp.prototype.exec()")}}</dt> + <dd>在该字符串中执行匹配项的搜索。</dd> + <dt>{{JSxRef("RegExp.prototype.test()")}}</dt> + <dd>该正则在字符串里是否有匹配。</dd> + <dt>{{JSxRef("RegExp.prototype.@@match()", "RegExp.prototype[@@match]()")}}</dt> + <dd>对给定字符串执行匹配并返回匹配结果。</dd> + <dt>{{JSxRef("RegExp.prototype.@@matchAll()", "RegExp.prototype[@@matchAll]()")}}</dt> + <dd>对给定字符串执行匹配,返回所有匹配结果。</dd> + <dt>{{JSxRef("RegExp.prototype.@@replace()", "RegExp.prototype[@@replace]()")}}</dt> + <dd>给定新的子串,替换所有匹配结果。</dd> + <dt>{{JSxRef("RegExp.prototype.@@search()", "RegExp.prototype[@@search]()")}}</dt> + <dd>在给定字符串中搜索匹配项,并返回在字符串中找到字符索引。</dd> + <dt>{{JSxRef("RegExp.prototype.@@split()", "RegExp.prototype[@@split]()")}}</dt> + <dd>通过将给定字符串拆分为子字符串,并返回字符串形成的数组。</dd> + <dt>{{JSxRef("RegExp.prototype.toString()")}}</dt> + <dd>返回表示指定对象的字符串。重写{{jsxref("Object.prototype.toString()")}}方法。</dd> +</dl> + +<h2 id="Syntax" name="Syntax">示例</h2> + +<h3 id="Using_a_regular_expression_to_change_data_format" name="Using_a_regular_expression_to_change_data_format">使用正则改变数据结构</h3> + +<p>下例使用 {{jsxref("Global_Objects/String", "String")}} 的 {{jsxref("String.prototype.replace()", "replace()")}} 方法去匹配姓名 <em>first last </em>输出新的格式 <em>last</em>, <em>first</em>。</p> + +<p>在替换的文本中,脚本中使用 <code>$1</code> 和 <code>$2</code> 指明括号里先前的匹配.</p> + +<pre class="brush: js notranslate">let re = /(\w+)\s(\w+)/; +let str = "John Smith"; +let newstr = str.replace(re, "$2, $1"); +console.log(newstr); +</pre> + +<p>这将显示 "Smith, John".</p> + +<h3 id="使用正则来划分带有多种行结束符和换行符的文本">使用正则来划分带有多种行结束符和换行符的文本</h3> + +<p>对于不同的平台(Unix,Windows等等),其默认的行结束符是不一样的. 而下面的划分方式适用于所有平台。</p> + +<pre class="notranslate">let text = 'Some text\nAnd some more\r\nAnd yet\rThis is the end' +let lines = text.split(/\r\n|\r|\n/) +console.log(lines) // logs [ 'Some text', 'And some more', 'And yet', 'This is the end' ] +</pre> + +<p>注意:在正则表达式中,以竖线分割的子模式的顺序会影响匹配结果。</p> + +<h3 id="在多行文本中使用正则表达式">在多行文本中使用正则表达式</h3> + +<pre class="brush: js notranslate">let s = "Please yes\nmake my day!"; + +s.match(/yes.*day/); +// Returns null + +s.match(/yes[^]*day/); +// Returns 'yes\nmake my day'</pre> + +<h3 id="Using_a_regular_expression_with_the_sticky_flag" name="Using_a_regular_expression_with_the_sticky_flag">使用带有 sticky 标志的正则表达式</h3> + +<p>带有{{JSxRef("Global_Objects/RegExp/sticky", "sticky")}}标志的正则表达式将会从源字符串的{{jsxref("RegExp.prototype.lastIndex")}}位置开始匹配,也就是进行“粘性匹配”。</p> + +<pre class="brush: js notranslate">let str = '#foo#' +let regex = /foo/y + +regex.lastIndex = 1 +regex.test(str) // true +regex.lastIndex = 5 +regex.test(str) // false (lastIndex is taken into account with sticky flag) +regex.lastIndex // 0 (reset after match failure)</pre> + +<h3 id="sticky_标志和_global_标志的不同点"> sticky 标志和 global 标志的不同点</h3> + +<p>如果正则表达式有粘性 <code>y</code> 标志,下一次匹配一定在 <code>lastIndex</code> 位置开始;如果正则表达式有全局 <code>g</code> 标志,下一次匹配可能在 <code>lastIndex</code> 位置开始,也可能在这个位置的后面开始。</p> + +<pre class="brush: js notranslate">re = /\d/y; +while (r = re.exec("123 456")) console.log(r, "AND re.lastIndex", re.lastIndex); + +// [ '1', index: 0, input: '123 456', groups: undefined ] AND re.lastIndex 1 +// [ '2', index: 1, input: '123 456', groups: undefined ] AND re.lastIndex 2 +// [ '3', index: 2, input: '123 456', groups: undefined ] AND re.lastIndex 3 +// ... and no more match.</pre> + +<p>如果使用带有全局标志<code>g</code>的正则表达式<code>re</code>,就会捕获字符串中的所有6个数字,而非3个</p> + +<h3 id="Browser_Compatibility" name="Browser_Compatibility">使用正则表达式和 Unicode 字符</h3> + +<p>正如上面表格提到的,<code>\w</code> 或 <code>\W</code> 只会匹配基本的 ASCII 字符;如 <code>a</code> 到 <code>z</code>、 <code>A</code> 到 <code>Z</code>、 <code>0</code> 到 <code>9</code> 及 <code>_</code>。</p> + +<p>为了匹配其他语言中的字符,如西里尔(Cyrillic)或 希伯来语(Hebrew),要使用 <code>\uhhhh</code>,<code>hhhh</code> 表示以十六进制表示的字符的 Unicode 值。</p> + +<p>下例展示了怎样从一个单词中分离出 Unicode 字符。</p> + +<pre class="brush: js notranslate">let text = "Образец text на русском языке"; +let regex = /[\u0400-\u04FF]+/g; + +let match = regex.exec(text); +console.log(match[1]); // prints "Образец" +console.log(regex.lastIndex); // prints "7" + +let match2 = regex.exec(text); +console.log(match2[1]); // prints "на" [did not print "text"] +console.log(regex.lastIndex); // prints "15" + +// and so on</pre> + +<p><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes">Unicode属性转义特性</a>引入了一种解决方案,它允许使用像\p{scx=Cyrl}这样简单的语句。这里有一个外部资源,用来获取 Unicode 中的不同区块范围:<a href="http://kourge.net/projects/regexp-unicode-block" title="http://kourge.net/projects/regexp-unicode-block">Regexp-unicode-block</a></p> + +<h3 id="从_URL_中提取子域名">从 URL 中提取子域名</h3> + +<pre class="brush: js notranslate">var url = "http://xxx.domain.com"; +console.log(/[^.]+/.exec(url)[0].substr(7)); // logs "xxx" + +</pre> + +<div class="blockIndicator note"> +<p>使用浏览器内建的<a href="/en-US/docs/Web/API/URL_API">URL API</a>而非正则表达式来解析URL是更好的做法</p> +</div> + +<h2 id="规范" style="margin-bottom: 20px; line-height: 30px;">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">备注</th> + </tr> + <tr> + <td>ECMAScript 1st Edition. Implemented in JavaScript 1.1</td> + <td>Standard</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.10', 'RegExp')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-regexp-regular-expression-objects', 'RegExp')}}</td> + <td>{{Spec2('ES6')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_Compatibility" name="Browser_Compatibility">浏览器兼容性</h2> + +<p>{{Compat("javascript.builtins.RegExp")}}</p> + +<h3 id="Firefox_特定版本提示">Firefox 特定版本提示</h3> + +<p>Starting with Gecko 34 {{geckoRelease(34)}}, in the case of a capturing group with quantifiers preventing its exercise, the matched text for a capturing group is now <code>undefined</code> instead of an empty string:</p> + +<pre class="notranslate"><code>// Firefox 33 or older +'x'.replace(/x(.)?/g, function(m, group) { + console.log("'group:" + group + "'"); +}); // 'group:' + +// Firefox 34 or newer +'x'.replace(/x(.)?/g, function(m, group) { + console.log("'group:" + group + "'"); +}); // 'group:undefined'</code></pre> + +<p>注意,由于web兼容性 <code>RegExp.$N</code> 仍会返回一个空的字符串代替 <code>undefined</code> ({{bug(1053944)}}).</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li><a href="https://developer.mozilla.org/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>一节</li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match">String.prototype.match()</a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace">String.prototype.replace()</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/input/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/input/index.html new file mode 100644 index 0000000000..87b9ecb657 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/input/index.html @@ -0,0 +1,99 @@ +--- +title: RegExp.input ($_) +slug: Web/JavaScript/Reference/Global_Objects/RegExp/input +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/input +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p><strong><code>input</code> </strong>非标准属性是正则表达式静态属性,含有正则表达式所匹配的字符串。<code>RegExp.$_</code>是这个属性的别名。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><var>RegExp</var>.input +RegExp.$_ +</pre> + +<h2 id="描述">描述</h2> + +<p><code>input</code> 属性是静态的,并不是正则表达式独立对象的属性。反之,你应始终将其使用为 <code>RegExp.input</code> 或者 <code>RegExp.$_。</code></p> + +<p>当正则表达式上搜索的字符串发生改变,并且字符串匹配时,<code><strong>input</strong></code> 属性的值会修改。</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_input_和__">使用 <code>input</code> 和 <code>$_</code></h3> + +<pre class="brush: js">var re = /hi/g; +re.test('hi there!'); +RegExp.input; // "hi there!" +re.test('foo'); // 新测试,不匹配 +RegExp.$_; // "hi there!" +re.test('hi world!'); // 新测试,匹配 +RegExp.$_; // "hi world!" +</pre> + +<h2 id="规范">规范</h2> + +<p>非标准。并不是任何现行规范的一部分。</p> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{non-standard_inline}} {{jsxref("RegExp.lastMatch", "RegExp.lastMatch ($&)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.lastParen", "RegExp.lastParen ($+)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.leftContext", "RegExp.leftContext ($`)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.rightContext", "RegExp.rightContext ($')")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.n", "RegExp.$1-$9")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/lastindex/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/lastindex/index.html new file mode 100644 index 0000000000..4931537cb6 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/lastindex/index.html @@ -0,0 +1,128 @@ +--- +title: RegExp.lastIndex +slug: Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex +tags: + - JavaScript + - Property + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex +--- +<div> + {{JSRef("Global_Objects", "RegExp")}}</div> +<h2 id="Summary" name="Summary" style="margin-bottom: 20px; line-height: 30px;">概述</h2> +<p><code><strong>lastIndex</strong></code> 是正则表达式的一个可读可写的整型属性,用来指定下一次匹配的起始索引。</p> +<div> + {{js_property_attributes(1,0,0)}}</div> +<h2 id="语法" style="margin-bottom: 20px; line-height: 30px;">语法</h2> +<pre class="syntaxbox language-html" style="margin-bottom: 0px; padding: 1em; border-left-width: 6px; border-left-style: solid; font-family: Consolas, Monaco, 'Andale Mono', monospace; font-size: 14px; direction: ltr; white-space: normal; text-shadow: none; background-color: rgba(212, 221, 228, 0.498039);"><var>lastIndex</var> = <var>regExpObj</var>.lastIndex;</pre> +<h2 id="Description" name="Description" style="margin-bottom: 20px; line-height: 30px;">描述</h2> +<p>只有正则表达式使用了表示全局检索的 "<code>g</code>" 标志时,该属性才会起作用。此时应用下面的规则:</p> +<ul> + <li>如果 <code>lastIndex</code> 大于字符串的长度,则 <code>regexp.test</code> 和 <code>regexp.exec</code> 将会匹配失败,然后 <code>lastIndex</code> 被设置为 0。</li> + <li>如果 <code>lastIndex</code> 等于字符串的长度,且该正则表达式匹配空字符串,则该正则表达式匹配从 <code>lastIndex</code> 开始的字符串。(then the regular expression matches input starting at <code style="font-style: normal;">lastIndex</code>.)</li> + <li>如果 <code>lastIndex</code> 等于字符串的长度,且该正则表达式不匹配空字符串 ,则该正则表达式不匹配字符串,<code>lastIndex</code> 被设置为 0.。</li> + <li>否则,<code>lastIndex</code> 被设置为紧随最近一次成功匹配的下一个位置。</li> +</ul> +<h2 id="示例" style="margin-bottom: 20px; line-height: 30px;">示例</h2> +<p>考虑下面的语句:</p> +<pre class="brush: js">var re = /(hi)?/g;</pre> +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 0px; background: 0px 0px;"> + </div> +<p>匹配空字符串</p> +<pre class="brush: js">console.log(re.exec("hi")); +console.log(re.lastIndex);</pre> +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 0px; background: 0px 0px;"> + </div> +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 19px; background: 0px 0px;"> + </div> +<p>返回 <code>["hi", "hi"]</code> ,<code>lastIndex</code> 等于 2。</p> +<pre class="brush: js">console.log(re.exec("hi")); +console.log(re.lastIndex);</pre> +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 0px; background: 0px 0px;"> + </div> +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 19px; background: 0px 0px;"> + </div> +<p>返回 <code>["", undefined]</code>,即一个数组,其第 0 个元素为匹配的字符串。此种情况下为空字符串,是因为 <code>lastIndex</code> 为 2(且一直是 2),"<code>hi</code>" 长度为 2。</p> +<h2 id="规范" style="margin-bottom: 20px; line-height: 30px;">规范</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>ECMAScript 3rd Edition. Implemented in JavaScript 1.2.</td> + <td>Standard</td> + <td>Initial definition.<br> + JavaScript 1.5: <code>lastIndex</code> is a property of a <code>RegExp</code> instance, not the <code>RegExp</code> object.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.10.7.5', 'RegExp.lastIndex')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-properties-of-regexp-instances', 'RegExp.lastIndex')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> +<h2 id="浏览器兼容性" style="margin-bottom: 20px; line-height: 30px;">浏览器兼容性</h2> +<p>{{ CompatibilityTable() }}</p> +<div id="compat-desktop"> + <table class="compat-table"> + <tbody> + <tr> + <th style="line-height: 16px;">Feature</th> + <th style="line-height: 16px;">Chrome</th> + <th style="line-height: 16px;">Firefox (Gecko)</th> + <th style="line-height: 16px;">Internet Explorer</th> + <th style="line-height: 16px;">Opera</th> + <th style="line-height: 16px;">Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<div id="compat-mobile"> + <table class="compat-table"> + <tbody> + <tr> + <th style="line-height: 16px;">Feature</th> + <th style="line-height: 16px;">Android</th> + <th style="line-height: 16px;">Chrome for Android</th> + <th style="line-height: 16px;">Firefox Mobile (Gecko)</th> + <th style="line-height: 16px;">IE Mobile</th> + <th style="line-height: 16px;">Opera Mobile</th> + <th style="line-height: 16px;">Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<h2 id="See_also" name="See_also" style="margin-bottom: 20px; line-height: 30px;">相关链接</h2> +<ul> + <li>{{jsxref("RegExp.prototype.ignoreCase")}}</li> + <li>{{jsxref("RegExp.prototype.global")}}</li> + <li>{{jsxref("RegExp.prototype.multiline")}}</li> + <li>{{jsxref("RegExp.prototype.source")}}</li> + <li>{{jsxref("RegExp.prototype.sticky")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/lastmatch/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/lastmatch/index.html new file mode 100644 index 0000000000..71a49daf44 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/lastmatch/index.html @@ -0,0 +1,98 @@ +--- +title: RegExp.lastMatch ($&) +slug: Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p><strong>lastMatch </strong>非标准属性是正则表达式的静态和只读属性,含有最后匹配到的字符串。<code>RegExp.$&</code> 是这个属性的别名。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><var>RegExp</var>.lastMatch +RegExp['$&'] +</pre> + +<h2 id="描述">描述</h2> + +<p><code>lastMatch</code> 属性是静态的,不是正则表达式独立对象的属性。反之,你应始终将其使用为 <code>RegExp.lastMatch</code> 或者 <code>RegExp['$&']</code>。</p> + +<p><code>lastMatch</code> 属性的值是只读的,并且会在匹配成功时修改。</p> + +<p>你不能使用属性访问器(<code>RegExp.$&</code>)来使用简写的别名,因为解析器在这里会将 "&" 看做表达式,并抛出 {{jsxref("SyntaxError")}} 。使用 <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors">方括号符号</a>来访问属性。</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_lastMatch_和">使用 <code>lastMatch</code> 和 <code>$&</code></h3> + +<pre class="brush: js">var re = /hi/g; +re.test('hi there!'); +RegExp.lastMatch; // "hi" +RegExp['$&']; // "hi" +</pre> + +<h2 id="规范">规范</h2> + +<p>非标准。并不是任何现行规范的一部分。</p> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{non-standard_inline}} {{jsxref("RegExp.input", "RegExp.input ($_)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.lastParen", "RegExp.lastParen ($+)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.leftContext", "RegExp.leftContext ($`)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.rightContext", "RegExp.rightContext ($')")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.n", "RegExp.$1-$9")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/lastparen/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/lastparen/index.html new file mode 100644 index 0000000000..0424c58ab3 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/lastparen/index.html @@ -0,0 +1,98 @@ +--- +title: RegExp.lastParen ($+) +slug: Web/JavaScript/Reference/Global_Objects/RegExp/lastParen +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/lastParen +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p><strong>lastParen </strong>非标准属性是正则表达式的静态和只读属性,包含匹配到的最后一个子串(如果存在)。<code>RegExp.$+</code>是这一属性的别名。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><var>RegExp</var>.lastParen +RegExp['$+'] +</pre> + +<h2 id="描述">描述</h2> + +<p><code>lastParen</code> 属性是静态的,不是正则表达式独立对象的属性。反之,你应始终将其使用为 <code>RegExp.lastParen</code> 或者 <code>RegExp['$+']</code>。</p> + +<p><code>lastParen</code> 属性的值是只读的,并且会在匹配成功时修改。</p> + +<p>你不能使用属性访问器(<code>RegExp.$+</code>)来使用简写的别名,因为解析器在这里会将 "+" 看做表达式,并抛出 {{jsxref("SyntaxError")}} 。使用 <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors">方括号符号</a>来访问属性。</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_lastParen_和">使用 <code>lastParen</code> 和 <code>$+</code></h3> + +<pre class="brush: js">var re = /(hi)/g; +re.test('hi there!'); +RegExp.lastParen; // "hi" +RegExp['$+']; // "hi" +</pre> + +<h2 id="规范">规范</h2> + +<p>非标准。并不是任何现行规范的一部分。</p> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{non-standard_inline}} {{jsxref("RegExp.input", "RegExp.input ($_)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.lastMatch", "RegExp.lastMatch ($&)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.leftContext", "RegExp.leftContext ($`)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.rightContext", "RegExp.rightContext ($')")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.n", "RegExp.$1-$9")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/leftcontext/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/leftcontext/index.html new file mode 100644 index 0000000000..b2bf7596ce --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/leftcontext/index.html @@ -0,0 +1,98 @@ +--- +title: RegExp.leftContext ($`) +slug: Web/JavaScript/Reference/Global_Objects/RegExp/leftContext +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/leftContext +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p><strong>leftContext </strong>非标准属性是正则表达式的静态和只读属性,含有最新匹配的左侧子串。 <code>RegExp.$`</code> 是这个属性的别名。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><var>RegExp</var>.leftContext +RegExp['$`'] +</pre> + +<h2 id="描述">描述</h2> + +<p><code>leftContext</code> 属性是静态的,不是正则表达式独立对象的属性。反之,你应始终将其使用为 <code>RegExp.leftContext</code> 或者 <code>RegExp['$`']</code>。</p> + +<p><code>leftContext</code> 属性的值是只读的,并且会在匹配成功时修改。</p> + +<p>你不能使用属性访问器(<code>RegExp.$`</code>)来使用简写的别名,因为解析器在这里会将其看做模板字符串的开始,并抛出 {{jsxref("SyntaxError")}} 。使用 <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors">方括号符号</a>来访问属性。</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_leftContext_和">使用 <code>leftContext</code> 和 <code>$`</code></h3> + +<pre class="brush: js">var re = /world/g; +re.test('hello world!'); +RegExp.leftContext; // "hello " +RegExp['$`']; // "hello " +</pre> + +<h2 id="规范">规范</h2> + +<p>非标准。并不是任何现行规范的一部分。</p> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{non-standard_inline}} {{jsxref("RegExp.input", "RegExp.input ($_)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.lastMatch", "RegExp.lastMatch ($&)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.lastParen", "RegExp.lastParen ($+)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.rightContext", "RegExp.rightContext ($')")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.n", "RegExp.$1-$9")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/multiline/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/multiline/index.html new file mode 100644 index 0000000000..8f9a9adbd6 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/multiline/index.html @@ -0,0 +1,108 @@ +--- +title: RegExp.prototype.multiline +slug: Web/JavaScript/Reference/Global_Objects/RegExp/multiline +tags: + - JavaScript + - Property + - Prototype + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/multiline +--- +<div> + {{JSRef("Global_Objects", "RegExp")}}</div> +<h2 id="Summary" name="Summary">概述</h2> +<p><code><strong>multiline</strong></code> 属性表明正则表达式是否使用了 "<code>m</code>" 标志。<code>multiline</code> 是正则表达式实例的一个只读属性。</p> +<div> + {{js_property_attributes(0,0,0)}}</div> +<h2 id="Description" name="Description">描述</h2> +<p><code>multiline</code> 是一个布尔对象,如果使用了 "<code>m</code>" 标志,则返回 <code>true</code>;否则,返回 <code>false</code>。"<code>m</code>" 标志意味着一个多行输入字符串被看作多行。例如,使用 "<code>m</code>","<code>^</code>" 和 "<code>$</code>" 将会从只匹配正则字符串的开头或结尾,变为匹配字符串中任一行的开头或结尾。</p> +<p>你无法直接更改此属性。</p> +<h2 id="Examples" name="Examples">示例</h2> +<h3 id="Example:_Using_MIN_VALUE" name="Example:_Using_MIN_VALUE">例子:使用 <code>multiline</code></h3> +<pre class="brush:js">var regex = new RegExp("foo", "m") + +console.log(regex.multiline) // true +</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>ECMAScript 3rd Edition. Implemented in JavaScript 1.2.</td> + <td>Standard</td> + <td>Initial definition.<br> + JavaScript 1.5: <code>multiline</code> is a property of a <code>RegExp</code> instance, not the <code>RegExp</code> object.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.10.7.4', 'RegExp.prototype.multiline')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-get-regexp.prototype.multiline', 'RegExp.prototype.multiline')}}</td> + <td>{{Spec2('ES6')}}</td> + <td><code>multiline</code> is now a prototype accessor property rather than an instance's own data property.</td> + </tr> + </tbody> +</table> +<h2 id="浏览器兼容性">浏览器兼容性</h2> +<p>{{ CompatibilityTable() }}</p> +<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>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</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>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<h2 id="See_also" name="See_also">相关链接</h2> +<ul> + <li>{{jsxref("RegExp.prototype.global")}}</li> + <li>{{jsxref("RegExp.prototype.lastIndex")}}</li> + <li>{{jsxref("RegExp.prototype.ignoreCase")}}</li> + <li>{{jsxref("RegExp.prototype.source")}}</li> + <li>{{jsxref("RegExp.prototype.sticky")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/n/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/n/index.html new file mode 100644 index 0000000000..5101f73b28 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/n/index.html @@ -0,0 +1,110 @@ +--- +title: RegExp.$1-$9 +slug: Web/JavaScript/Reference/Global_Objects/RegExp/n +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/n +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p>非标准<strong>$1, $2, $3, $4, $5, $6, $7, $8, $9 </strong>属性是包含括号子串匹配的正则表达式的静态和只读属性。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><var>RegExp</var>.$1 +RegExp.$2</code> +RegExp.$3 +RegExp.$4 +RegExp.$5 +RegExp.$6 +RegExp.$7 +RegExp.$8 +RegExp.$9 +</pre> + +<h2 id="描述">描述</h2> + +<p>$1, ..., $9 属性是静态的, 他不是独立的的正则表达式属性. 所以, 我们总是像这样子使用他们<code>RegExp.$1</code>, ..., <code>RegExp.$9</code>.</p> + +<p>属性的值是只读的而且只有在正确匹配的情况下才会改变.</p> + +<p>括号匹配项是无限的, 但是RegExp对象能捕获的只有九个. 你可以通过返回一个数组索引来取得所有的括号匹配项.</p> + +<p>这些属性可以在{{jsxref("String.replace")}} 方法中替换字符串. 在这种情况下, 不用在前面加上RegExp。下面的例子将详细说明. 当正则表达式中不包含括号, 脚本中的 <code>$n</code>'s 就是字面上的意思 (当n是正整数).</p> + +<h2 id="例子">例子</h2> + +<h3 id="n_在_String.replace中的应用"><code>$n</code> 在 <code>String.replace中的应用</code></h3> + +<p>以下脚本用 {{jsxref("String.prototype.replace()", "replace()")}} 方法去匹配一个first last格式的 name{{jsxref("String")}} 实例 输出last first格式. 在替换文本里, 脚本用 <code>$1</code> 和 <code>$2</code> 表示正则表达式中的括号匹配项的结果.</p> + +<pre class="brush: js">var re = /(\w+)\s(\w+)/; +var str = 'John Smith'; +str.replace(re, '$2, $1'); // "Smith, John" +RegExp.$1; // "John" +RegExp.$2; // "Smith" +</pre> + +<h2 id="技术指标">技术指标</h2> + +<p>非标准. 不属于当前的任何规范.</p> + +<h2 id="浏览器适配">浏览器适配</h2> + +<div>{{CompatibilityTable}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{non-standard_inline}} {{jsxref("RegExp.input", "RegExp.input ($_)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.lastMatch", "RegExp.lastMatch ($&)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.lastParen", "RegExp.lastParen ($+)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.leftContext", "RegExp.leftContext ($`)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.rightContext", "RegExp.rightContext ($')")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/prototype/index.html new file mode 100644 index 0000000000..e841c0e72d --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/prototype/index.html @@ -0,0 +1,152 @@ +--- +title: RegExp.prototype +slug: Web/JavaScript/Reference/Global_Objects/RegExp/prototype +tags: + - JavaScript + - Property + - RegExp +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp +--- +<p>{{JSRef("Global_Objects", "RegExp")}}</p> +<h2 id="Summary" name="Summary">概述</h2> +<p><font face="Courier New, Andale Mono, monospace">RegExp.prototype 属性表示 </font><span style="line-height: 1.5;">{{jsxref("Global_Objects/RegExp", "RegExp")}} </span><span style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: 1.5;">构造函数的原型对象。</span></p> +<h2 id="Description" name="Description">描述</h2> +<p>查看<font face="Courier New, Andale Mono, monospace"> </font><span style="line-height: 1.5;">{{jsxref("Global_Objects/RegExp", "RegExp")}} </span><span style="line-height: 1.5;">了解更多关于 </span><code style="font-style: normal; line-height: 1.5;">RegExp</code> <code style="font-style: normal; line-height: 1.5;">实例的说明。</code></p> +<p><code>RegExp</code> 实例继承 <code>RegExp.prototype</code>。修改该原型对象上的属性或方法会影响到所有的 <code>RegExp</code> <code>实例。</code></p> +<h2 id="Properties" name="Properties">属性</h2> +<p>查看<a href="/zh-CN/docs/JavaScript/Reference/Deprecated_Features#RegExp_Properties" title="zh-CN/docs/JavaScript/Reference/Deprecated_Features#RegExp_Properties">已废弃的RegExp属性</a></p> +<p>注意,<code>RegExp</code> 对象的几个属性既有完整的长属性名,也有对应的类 Perl 的短属性名。两个属性都有着同样的值。JavaScript 的正则语法就是基于 Perl 的。</p> +<dl> + <dt> + <code style="font-style: normal; font-weight: bold;">RegExp.prototype.</code><code style="font-style: normal; font-weight: bold;">constructor</code></dt> + <dd> + 创建该正则对象的构造函数。</dd> + <dt> + {{jsxref("RegExp.prototype.global")}}</dt> + <dd> + 是否开启全局匹配,也就是匹配目标字符串中所有可能的匹配项,而不是只进行第一次匹配。</dd> + <dt> + {{jsxref("RegExp.prototype.ignoreCase")}}</dt> + <dd> + 在匹配字符串时是否要忽略字符的大小写。</dd> + <dt> + {{jsxref("RegExp.prototype.lastIndex")}}</dt> + <dd> + 下次匹配开始的字符串索引位置。</dd> + <dt> + {{jsxref("RegExp.prototype.multiline")}}</dt> + <dd> + 是否开启多行模式匹配(影响 ^ 和 $ 的行为)。</dd> + <dt> + {{jsxref("RegExp.prototype.source")}}</dt> + <dd> + 正则对象的源模式文本。</dd> + <dt> + {{jsxref("RegExp.prototype.sticky")}} {{experimental_inline}}</dt> + <dd> + 是否开启粘滞匹配。</dd> +</dl> +<div> + {{ jsOverrides("Object", "properties", "constructor", "global", "ignoreCase", "lastIndex", "multiline", "source", "sticky") }}</div> +<h2 id="Methods" name="Methods">方法</h2> +<p>查看<a href="/zh-CN/docs/JavaScript/Reference/Deprecated_Features#RegExp_Methods" title="zh-CN/docs/JavaScript/Reference/Deprecated_Features#RegExp_Methods">已废弃的RegExp方法</a></p> +<dl> + <dt> + {{jsxref("RegExp.prototype.exec()")}}</dt> + <dd> + 在目标字符串中执行一次正则匹配操作。</dd> + <dt> + {{jsxref("RegExp.prototype.test()")}}</dt> + <dd> + 测试当前正则是否能匹配目标字符串。</dd> + <dt> + {{jsxref("RegExp.prototype.toSource()")}} {{non-standard_inline}}</dt> + <dd> + 返回一个字符串,其值为该正则对象的字面量形式。覆盖了<code>Object.prototype.toSource</code> 方法.</dd> + <dt> + {{jsxref("RegExp.prototype.toString()")}}</dt> + <dd> + 返回一个字符串,其值为该正则对象的字面量形式。覆盖了{{jsxref("Object.prototype.toString()")}} 方法。</dd> +</dl> +<div> + {{ jsOverrides("Object", "Methods", "exec", "test", "toSource", "toString") }}</div> +<h2 id="规范" style="margin-bottom: 20px; line-height: 30px;">规范</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>ECMAScript 1st Edition. Implemented in JavaScript 1.1</td> + <td>Standard</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.10.5.1', 'RegExp')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-regexp.prototype', 'RegExp.prototype')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> +<h2 id="浏览器兼容性" style="margin-bottom: 20px; line-height: 30px;">浏览器兼容性</h2> +<p>{{ CompatibilityTable() }}</p> +<div id="compat-desktop"> + <table class="compat-table"> + <tbody> + <tr> + <th style="line-height: 16px;">Feature</th> + <th style="line-height: 16px;">Chrome</th> + <th style="line-height: 16px;">Firefox (Gecko)</th> + <th style="line-height: 16px;">Internet Explorer</th> + <th style="line-height: 16px;">Opera</th> + <th style="line-height: 16px;">Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<div id="compat-mobile"> + <table class="compat-table"> + <tbody> + <tr> + <th style="line-height: 16px;">Feature</th> + <th style="line-height: 16px;">Android</th> + <th style="line-height: 16px;">Chrome for Android</th> + <th style="line-height: 16px;">Firefox Mobile (Gecko)</th> + <th style="line-height: 16px;">IE Mobile</th> + <th style="line-height: 16px;">Opera Mobile</th> + <th style="line-height: 16px;">Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<h2 id="相关链接" style="margin-bottom: 20px; line-height: 30px;">相关链接</h2> +<ul> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions" title="JavaScript/Guide/Regular_Expressions">Regular Expressions</a> chapter in the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide" title="JavaScript/Guide">JavaScript Guide</a></li> + <li>{{jsxref("Global_Objects/RegExp", "RegExp")}}</li> + <li>{{jsxref("Function.prototype")}}</li> +</ul> +<p> </p> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/regexp/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/regexp/index.html new file mode 100644 index 0000000000..138ebf4ba7 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/regexp/index.html @@ -0,0 +1,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 notranslate">/<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>The text of the regular expression.</dd> + <dd>As of ES5, this can also be another <code>RegExp</code> object or literal (for the two RegExp constructor notations only). Patterns may include <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Using_special_characters">special characters</a> to match a wider range of values than would a literal string. </dd> + <dt><code><var>flags</var></code></dt> + <dd> + <p>If specified, <code><var>flags</var></code> is a string that contains the flags to add.</p> + + <p>Alternatively, if an object is supplied for the pattern, the <code><var>flags</var></code> string will replace any of that object's flags (and <code>lastIndex</code> will be reset to <code>0</code>) (as of ES2015).</p> + + <p>If <code><var>flags</var></code> is not specified and a regular expressions object is supplied, that object's flags (and <code>lastIndex</code> value) will be copied over.</p> + + <p><code>flags</code> may contain any combination of the following characters:</p> + + <dl> + <dt><code>g</code> (全局匹配)</dt> + <dd>Find all matches rather than stopping after the first match.</dd> + <dt><code>i</code> (忽略大小写)</dt> + <dd>If <code>u</code> flag is also enabled, use Unicode case folding.</dd> + <dt><code>m</code> (多行匹配)</dt> + <dd>Treat beginning and end characters (<code>^</code> and <code>$</code>) as working over multiple lines. In other words, match the beginning or end of <em>each</em> line (delimited by <code>\n</code> or <code>\r</code>), not only the very beginning or end of the whole input string.</dd> + <dt><code>s</code> (点号匹配所有字符)</dt> + <dd>Allows <code>.</code> to match newlines.</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>There are two ways to create a <code>RegExp</code> object: a <em>literal notation</em> and a <em>constructor</em>.</p> + +<ul> + <li><strong>The literal notation's</strong> parameters are enclosed between slashes and do not use quotation marks.</li> + <li><strong>The constructor function's</strong> parameters are not enclosed between slashes but do use quotation marks.</li> +</ul> + +<p>The following three expressions create the same regular expression:</p> + +<pre class="brush: js notranslate">/ab+c/i +new RegExp(/ab+c/, 'i') // literal notation +new RegExp('ab+c', 'i') // constructor +</pre> + +<p>The literal notation results in compilation of the regular expression when the expression is evaluated. Use literal notation when the regular expression will remain constant. For example, if you use literal notation to construct a regular expression used in a loop, the regular expression won't be recompiled on each iteration.</p> + +<p>The constructor of the regular expression object—for example, <code>new RegExp('ab+c')</code>—results in runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</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> +<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>一节 + <li>{{jsxref("String.prototype.match()")}}</li> + <li>{{jsxref("String.prototype.replace()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/rightcontext/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/rightcontext/index.html new file mode 100644 index 0000000000..5426301369 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/rightcontext/index.html @@ -0,0 +1,98 @@ +--- +title: RegExp.rightContext ($') +slug: Web/JavaScript/Reference/Global_Objects/RegExp/rightContext +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/rightContext +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p><strong>rightContext </strong>非标准属性是正则表达式的静态和只读属性,含有最新匹配的右侧子串。 <code>RegExp.$'</code> 是这个属性的别名。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><var>RegExp</var>.rightContext +RegExp["$'"]</code> +</pre> + +<h2 id="描述">描述</h2> + +<p><code>rightContext</code> 属性是静态的,不是正则表达式独立对象的属性。反之,你应始终将其使用为 <code>RegExp.rightContext</code> 或者 <code>RegExp["$'"]</code>。</p> + +<p><code>rightContext</code> 属性的值是只读的,并且会在匹配成功时修改。</p> + +<p>你不能使用属性访问器(<code>RegExp.$'</code>)来使用简写的别名,因为解析器在这里会将其看做字符串的开始,并抛出 {{jsxref("SyntaxError")}}。使用 <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors">方括号符号</a>来访问属性。</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_rightContext_和_'">使用 <code>rightContext</code> 和 <code>$'</code></h3> + +<pre class="brush: js">var re = /hello/g; +re.test('hello world!'); +RegExp.rightContext; // " world!" +RegExp["$'"]; // " world!" +</pre> + +<h2 id="规范">规范</h2> + +<p>非标准。并不是任何现行规范的一部分。</p> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{non-standard_inline}} {{jsxref("RegExp.input", "RegExp.input ($_)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.lastMatch", "RegExp.lastMatch ($&)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.lastParen", "RegExp.lastParen ($+)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.leftContext", "RegExp.leftContext ($`)")}}</li> + <li>{{non-standard_inline}} {{jsxref("RegExp.n", "RegExp.$1-$9")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/source/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/source/index.html new file mode 100644 index 0000000000..8f526e39ca --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/source/index.html @@ -0,0 +1,109 @@ +--- +title: RegExp.prototype.source +slug: Web/JavaScript/Reference/Global_Objects/RegExp/source +tags: + - JavaScript + - Property + - Prototype + - Reference + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/source +--- +<div>{{JSRef("Global_Objects", "RegExp")}}</div> + +<h2 id="Summary" name="Summary">概述</h2> + +<p><strong><code>source</code></strong> 属性返回一个值为当前正则表达式对象的模式文本的字符串,该字符串不会包含正则字面量两边的斜杠以及任何的标志字符。</p> + +<h2 id="Examples" name="Examples">示例</h2> + +<pre class="brush: js">var regex = /fooBar/ig; + +console.log(regex.source); // "fooBar",不包含 /.../ 和 "ig"。 +</pre> + +<h2 id="Specifications" name="Specifications">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范名称</th> + <th scope="col">规范状态</th> + <th scope="col">备注</th> + </tr> + <tr> + <td>ECMAScript 3rd Edition.</td> + <td>Standard</td> + <td><code>source</code> 是正则对象自身的数据属性</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.10.7.1', 'RegExp.prototype.source')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-get-regexp.prototype.source', 'RegExp.prototype.source')}}</td> + <td>{{Spec2('ES6')}}</td> + <td><code>source</code> 成为了正则对象原型上的一个访问器属性</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li>{{jsxref("RegExp.prototype.flags")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/sticky/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/sticky/index.html new file mode 100644 index 0000000000..f74b96499b --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/sticky/index.html @@ -0,0 +1,86 @@ +--- +title: RegExp.prototype.sticky +slug: Web/JavaScript/Reference/Global_Objects/RegExp/sticky +tags: + - JavaScript + - RegExp + - 正则表达式 +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/sticky +--- +<div>{{JSRef}}</div> + +<p><strong><code>sticky</code></strong> 属性反映了搜索是否具有粘性( 仅从正则表达式的 {{jsxref("RegExp.lastIndex", "lastIndex")}} 属性表示的索引处搜索 )。<code>sticky</code> 是正则表达式对象的只读属性。</p> + +<p>{{EmbedInteractiveExample("pages/js/regexp-prototype-sticky.html", "taller")}}</p> + +<div>{{js_property_attributes(0, 0, 1)}}</div> + +<h2 id="描述">描述</h2> + +<p><code>sticky</code> 的值是 {{jsxref("Boolean")}} ,并在 <code>y</code> 标志使用时为真; 否则为假。<code>y</code> 标志指示,仅从正则表达式的 {{jsxref("RegExp.lastIndex", "lastIndex")}} 属性表示的索引处为目标字符串匹配(并且不会尝试从后续索引匹配)。如果一个表达式同时指定了 <code>sticky</code> 和 <code>global</code>,其将会忽略 <code>global</code> 标志。</p> + +<p>你不能直接更改这个属性,它是只读的。</p> + +<h2 id="例子">例子</h2> + +<h3 id="使用带_sticky_标志的正则表达式">使用带 sticky 标志的正则表达式</h3> + +<pre class="brush: js">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 (匹配失败后重置) +</pre> + +<h3 id="锚定的_sticky_标志">锚定的 sticky 标志</h3> + +<p>火狐的 SpiderMonkey 引擎的几个版本有一个 <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=773687">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 href="http://www.ecma-international.org/ecma-262/7.0/index.html#sec-assertion">特别指出</a>:</p> + +<blockquote> +<p>当使用带有 <code>y</code> 标识的匹配模式时,^ 断言总是会匹配输入的开始位置或者(如果是多行模式)每一行的开始位置。</p> +</blockquote> + +<p>正确行为的示例:</p> + +<pre class="brush: js">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是行的开始 +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-get-regexp.prototype.sticky', 'RegExp.prototype.sticky')}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("javascript.builtins.RegExp.sticky")}}</p> + +<div></div> + +<h2 id="相关链接"><span style="">相关链接</span></h2> + +<ul> + <li>{{jsxref("RegExp.lastIndex")}}</li> + <li>{{jsxref("RegExp.prototype.global")}}</li> + <li>{{jsxref("RegExp.prototype.ignoreCase")}}</li> + <li>{{jsxref("RegExp.prototype.multiline")}}</li> + <li>{{jsxref("RegExp.prototype.source")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/test/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/test/index.html new file mode 100644 index 0000000000..d742f6c172 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/test/index.html @@ -0,0 +1,128 @@ +--- +title: RegExp.prototype.test() +slug: Web/JavaScript/Reference/Global_Objects/RegExp/test +tags: + - JavaScript + - Method + - Prototype + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/test +--- +<div>{{JSRef}}</div> + +<p><code><strong>test()</strong></code> 方法执行一个检索,用来查看正则表达式与指定的字符串是否匹配。返回 <code>true</code> 或 <code>false</code>。</p> + +<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-test.html", "taller")}}</div> + +<div class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="notranslate"><var>regexObj</var>.test(str)</pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<dl> + <dt><code>str</code></dt> + <dd>用来与正则表达式匹配的字符串</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>如果正则表达式与指定的字符串匹配 ,返回<code>true</code>;否则<code>false</code>。</p> + +<h2 id="Description" name="Description">描述</h2> + +<p>当你想要知道一个正则表达式是否与指定的字符串匹配时,就可以使用 <code>test()</code>(类似于<code> </code> {{jsxref("String.prototype.search()")}} 方法),差别在于test返回一个布尔值,而 search 返回索引(如果找到)或者-1(如果没找到);若想知道更多信息(然而执行比较慢),可使用{{jsxref("RegExp.prototype.exec()", "exec()")}} 方法(类似于 {{jsxref("String.prototype.match()")}} 方法)。 和 {{jsxref("RegExp.prototype.exec()", "exec()")}} (或者组合使用),一样,在相同的全局正则表达式实例上多次调用<code><code>test</code></code>将会越过之前的匹配。</p> + +<h2 id="Examples" name="Examples">示例</h2> + +<h3 id="Example_Using_test" name="Example:_Using_test">使用 <code>test()</code></h3> + +<p>一个简单的例子,测试 "hello" 是否包含在字符串的最开始,返回布尔值。</p> + +<pre class="brush: js notranslate">let str = 'hello world!'; +let result = /^hello/.test(str); +console.log(result); +// true +</pre> + +<p>下例打印一条信息,该信息内容取决于是否成功通过指定测试:</p> + +<pre class="brush: js notranslate">function testinput(re, str){ + var midstring; + if (re.test(str)) { + midstring = " contains "; + } else { + midstring = " does not contain "; + } + console.log(str + midstring + re.source); +} +</pre> + +<h3 id="当设置全局标志的正则使用test">当设置全局标志的正则使用<code>test()</code></h3> + +<p>如果正则表达式设置了全局标志,<code>test() </code>的执行会改变正则表达式 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex" title="The lastIndex is a read/write integer property of regular expression instances that specifies the index at which to start the next match."><code>lastIndex</code></a>属性。连续的执行<code>test()</code>方法,后续的执行将会从 lastIndex 处开始匹配字符串,({{jsxref("RegExp.prototype.exec()", "<code>exec()</code>")}} 同样改变正则本身的 <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex" title="The lastIndex is a read/write integer property of regular expression instances that specifies the index at which to start the next match.">lastIndex</a>属性值</code>).</p> + +<p>下面的实例表现了这种行为: </p> + +<pre class="brush: js notranslate">var regex = /foo/g; + +// regex.lastIndex is at 0 +regex.test('foo'); // true + +// regex.lastIndex is now at 3 +regex.test('foo'); // false +</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>{{SpecName('ES3')}}</td> + <td>{{Spec2('ES3')}}</td> + <td>Initial definition. Implemented in JavaScript 1.2.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.10.6.3', 'RegExp.test')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-regexp.prototype.test', 'RegExp.test')}}</td> + <td>{{Spec2('ES6')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp.prototype.test', 'RegExp.test')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h3 id="sect1"></h3> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div class="hidden">这个页面的兼容性表格是由结构数据生成。如果你想对数据作出贡献,详情请前往 <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并给我们一个PR。</div> + +<p>{{Compat("javascript.builtins.RegExp.test")}}</p> + +<h2 id="Firefox特殊注意">Firefox特殊注意</h2> + +<p>在 Firefox 8之前, <code>test()</code> 被不正确地实现了;当无参数地调用时,它会匹配之前的输入值 (RegExp.input 属性),而不是字符串"undefined"。这已经被修正了;现在 <code>/undefined/.test()</code> 正确地返回<code>true</code>,而不是错误。</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>在<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>章节</li> + <li>{{jsxref("RegExp")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/tosource/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/tosource/index.html new file mode 100644 index 0000000000..80f4972ca0 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/tosource/index.html @@ -0,0 +1,44 @@ +--- +title: RegExp.prototype.toSource() +slug: Web/JavaScript/Reference/Global_Objects/RegExp/toSource +tags: + - JavaScript + - Method + - Non-standard + - Prototype + - RegExp +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/toSource +--- +<p>{{JSRef("Global_Objects", "RegExp")}}{{ Non-standard_header() }}</p> + +<h2 id="Summary" name="Summary">概述</h2> + +<p>返回一个字符串,代表当前对象的源代码</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code><var>regexObj</var>.toSource() +</code></pre> + +<h2 id="Description" name="Description">描述</h2> + +<p><code>toSource</code>方法返回值如下:</p> + +<ul> + <li>对于内置的<code>RegExp</code>对象, <code>toSource</code> 如下字符串:</li> +</ul> + +<pre class="brush: js">function RegExp() { + [native code] +} +</pre> + +<ul> + <li>对于一个<code>RegExp实例</code>, <code>toSource返回代表该正则的字符串.</code></li> +</ul> + +<p>该方法通常由JavaScript内部隐含调用,而不会明确的出现在用户代码中.</p> + +<h2 id="See_Also" name="See_Also">相关链接</h2> + +<p><a href="/zh-cn/JavaScript/Reference/Global_Objects/Object/toSource" title="zh-cn/JavaScript/Reference/Global_Objects/Object/toSource">Object.prototype.toSource</a></p> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/tostring/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/tostring/index.html new file mode 100644 index 0000000000..22aef815b4 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/tostring/index.html @@ -0,0 +1,106 @@ +--- +title: RegExp.prototype.toString() +slug: Web/JavaScript/Reference/Global_Objects/RegExp/toString +tags: + - JavaScript + - Method + - Prototype + - RegExp +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/toString +--- +<div> + {{JSRef("Global_Objects", "RegExp")}}</div> +<h2 id="Summary" name="Summary">概述</h2> +<p><code><strong>toString()</strong></code> 返回一个表示该正则表达式的字符串。</p> +<h2 id="Syntax" name="Syntax">语法</h2> +<pre class="syntaxbox"><var>regexObj</var>.toString()</pre> +<h3 id="Parameters" name="Parameters">参数</h3> +<p>无</p> +<h2 id="Description" name="Description">描述</h2> +<p>{{jsxref("Global_Objects/RegExp", "RegExp")}} 对象覆盖了 {{jsxref("Global_Objects/Object", "Object")}} 对象的 <code>toString()</code> 方法,并没有继承 {{jsxref("Object.prototype.toString()")}}。对于 <code>RegExp</code> 对象,<code>toString</code> 方法返回一个该正则表达式的字符串形式。</p> +<h2 id="Examples" name="Examples">示例</h2> +<h3 id="Example:_Using_toString" name="Example:_Using_toString">例子:使用 <code>toString</code></h3> +<p>下例输出 <code>RegExp</code> 对象的字符串值:</p> +<pre>myExp = new RegExp("a+b+c"); +alert(myExp.toString()); // 显示 "/a+b+c/" + +foo = new RegExp("bar", "g"); +alert(foo.toString()); // 显示 "/bar/g" +</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>ECMAScript 3rd Edition. Implemented in JavaScript 1.1</td> + <td>Standard</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.9.5.2', 'RegExp.prototype.toString')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-regexp.prototype.tostring', 'RegExp.prototype.toString')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> +<h2 id="浏览器兼容性">浏览器兼容性</h2> +<p>{{ CompatibilityTable() }}</p> +<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>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</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>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<h2 id="See_Also" name="See_Also">相关链接</h2> +<ul> + <li>{{jsxref("Object.prototype.toString()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/unicode/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/unicode/index.html new file mode 100644 index 0000000000..bd4253824c --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/unicode/index.html @@ -0,0 +1,112 @@ +--- +title: RegExp.prototype.unicode +slug: Web/JavaScript/Reference/Global_Objects/RegExp/unicode +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/unicode +--- +<div>{{JSRef}}</div> + +<p><strong><code>unicode</code></strong> 属性表明正则表达式带有"<code>u</code>" 标志。 <code>unicode</code> 是正则表达式独立实例的只读属性。</p> + +<div>{{js_property_attributes(0, 0, 1)}}</div> + +<h2 id="描述">描述</h2> + +<p><code>unicode</code> 的值是 {{jsxref("Boolean")}},并且如果使用了 "<code>u</code>" 标志则为 <code>true</code>;否则为 <code>false</code>。"<code>u</code>" 标志开启了多种 Unicode 相关的特性。使用 "u" 标志,任何 Unicode 代码点的转义都会被解释。</p> + +<p>你不能直接修改这个属性,它是只读的。</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_unicode_属性">使用 <code>unicode</code> 属性</h3> + +<pre class="brush: js">var regex = new RegExp('\u{61}', 'u'); + +console.log(regex.unicode); // true +</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>{{SpecName('ES2015', '#sec-get-regexp.prototype.unicode', 'RegExp.prototype.unicode')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>初始定义。</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-get-regexp.prototype.unicode', 'RegExp.prototype.unicode')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</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>{{CompatChrome(50)}}</td> + <td>12 (case folding 13)</td> + <td>{{CompatGeckoDesktop(46)}}</td> + <td>{{CompatNo}}</td> + <td>37</td> + <td>10</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>{{CompatNo}}</td> + <td>{{CompatChrome(50)}}</td> + <td>{{CompatGeckoMobile(46)}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>10</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{jsxref("RegExp.lastIndex")}}</li> + <li>{{jsxref("RegExp.prototype.global")}}</li> + <li>{{jsxref("RegExp.prototype.ignoreCase")}}</li> + <li>{{jsxref("RegExp.prototype.multiline")}}</li> + <li>{{jsxref("RegExp.prototype.source")}}</li> + <li>{{jsxref("RegExp.prototype.sticky")}}</li> +</ul> |