> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/prim/str/method/str.matchall/matchall-using-named-groups.md).

# matchAll() using named groups

[JS](/web/js.md) ⟩ [primitives](/web/js/val/prim.md) ⟩ [String](/web/js/val/prim/str.md) ⟩ [methods](/web/js/val/prim/str/method.md) ⟩ [.matchAll()](/web/js/val/prim/str/method/str.matchall.md) ⟩ using named groups

{% tabs %}
{% tab title="💾 程式" %}
💾 程式：[replit](https://replit.com/@pegasusroe/strmatchAllregex)

```javascript
// ⭐️ pattern: yyyy-mm-dd (⭐️ using "named groups")              
//          ╭--- year ---╮ ╭--- month ---╮ ╭--- day ---╮
let date = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/g;

// text
let str = "2019-10-30, 2020-01-01, 18:24:00";

// ⭐️⭐️⭐️ result of `str.matchAll()` is an "iterable", NOT an array❗️
let matches = str.matchAll(date);

for (let match of matches) {
    let { year, month, day } = match.groups;    // ⭐️ object destruturing
    console.log(`year: ${year}, month: ${month}, day: ${day}`);
}

// output
// -------------------------------
// year: 2019, month: 10, day: 30
// year: 2020, month: 01, day: 01
```

{% endtab %}

{% tab title="👥 相關" %}

* [str.matchAll()](/web/js/val/prim/str/method/str.matchall.md) returns an [iterable iterator](/web/js/iteration/iterator/iterable.md).
* matching using [named group](/web/js/val/builtin/regex/pattern/group/capturing-group/named-group.md)s.
  {% endtab %}
  {% endtabs %}
