aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/regexp/@@matchall
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/regexp/@@matchall
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/regexp/@@matchall')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/regexp/@@matchall/index.html89
1 files changed, 89 insertions, 0 deletions
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">=&gt;</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>