`[@@matchAll]`方法返回对字符串使用正则表达式的所有匹配项。

{{EmbedInteractiveExample("pages/js/regexp-prototype-@@matchall.html")}}

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 https://github.com/mdn/interactive-examples and send us a pull request.

## 语法

    regexp[Symbol.matchAll](str)

### 参数

`str`
  一个`String`的匹配对象。

### 返回值

一个迭代器。

## 描述

本方法在`String.prototype.matchAll()`中被内部调用。例如，以下两个示例返回相同的结果。

    'abc'.matchAll(/a/);

    /a/[Symbol.matchAll]('abc');

本方法用于自定义`RegExp`子类中的匹配行为。

## 示例

### 直接调用

本方法的使用方法几乎与`String.prototype.matchAll()`相同，除了`this` 的不同以及参数顺序的的差异。

    var re = /[0-9]+/g;
    var str = '2016-01-02';
    var result = re[Symbol.matchAll](str);

    console.log(Array.from(result, x => x[0]));
    // ["2016", "01", "02"]

### 在子类中使用`@@matchAll`

`RegExp`的子类可以重写`[@@matchAll]()`方法来修改默认行为。例如，返回一个`Array`而不是iterator:

    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" ]

## 浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

Update compatibility data on GitHub

| Desktop | Mobile | Server
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | Safari on iOS | Samsung Internet | Node.js
`@@matchAll` | Chrome Full support 73 | Edge No support No | Firefox Full support 67 | IE No support No | Opera Full support 60 | Safari No support No | WebView Android Full support 73 | Chrome Android Full support 73 | Edge Mobile No support No | Firefox Android Full support 67 | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes | nodejs No support No

### Legend

Full support
  Full support
No support
  No support

## 相关链接

- `String.prototype.matchAll()`
- `Symbol.matchAll`