aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/regexp/@@matchall/index.html
blob: 3cc146570425218cf75d60ecde1b49f564762e69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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>