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
90
91
92
93
94
95
|
---
title: RegExp.prototype[@@matchAll]()
slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@matchAll
tags:
- JavaScript
- Méthode
- Prototype
- Reference
- RegExp
translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@matchAll
original_slug: Web/JavaScript/Reference/Objets_globaux/RegExp/@@matchAll
---
{{JSRef}}
La méthode **`[@@matchAll]`** renvoie l'ensemble des correspondances d'une expression rationnelle sur une chaîne de caractères.
{{EmbedInteractiveExample("pages/js/regexp-prototype-@@matchall.html")}}
## Syntaxe
regexp[Symbol.matchAll](str)
### Paramètres
- `str`
- : Une chaîne de caractères ({{jsxref("String")}}) dont on souhaite trouver les correspondances.
### Valeur de retour
Un [itérateur](/fr/docs/Web/JavaScript/Guide/iterateurs_et_generateurs).
## Description
Cette méthode est appelée, en interne, par le moteur JavaScript, pendant l'exécution {{jsxref("String.prototype.matchAll()")}}. Les deux lignes qui suivent renverront donc le même résultat.
```js
'abc'.matchAll(/a/);
/a/[Symbol.matchAll]('abc');
```
Cette méthode existe afin de personnaliser le comportement des correspondances pour les sous-classes de `RegExp`.
## Exemples
### Appel direct
Cette méthode peut être utilisée de façon semblable à {{jsxref("String.prototype.matchAll()")}} mais l'objet `this` et l'ordre des arguments seront différents.
```js
var re = /[0-9]+/g;
var str = '2016-01-02';
var resultat = re[Symbol.matchAll](str);
console.log(Array.from(resultat, x => x[0]));
// ["2016", "01", "02"]
```
### Utiliser `@@matchAll` dans une sous-classe
Les sous-classes de {{jsxref("RegExp")}} peuvent surcharger la méthode `[@@matchAll]()` afin de modifier le comportement par défaut (par exemple pour renvoyer un tableau ({{jsxref("Array")}}) plutôt qu'un [itérateur](/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators)).
```js
class MaRegExp extends RegExp {
[Symbol.matchAll](str) {
var resultat = RegExp.prototype[Symbol.matchAll].call(this, str);
if (!resultat) {
return null;
} else {
return Array.from(resultat);
}
}
}
var re = new MaRegExp('([0-9]+)-([0-9]+)-([0-9]+)', 'g');
var str = '2016-01-02|2019-03-07';
var resultat = str.matchAll(re);
console.log(resultat[0]); // [ "2016-01-02", "2016", "01", "02" ]
console.log(resultat[1]); // [ "2019-03-07", "2019", "03", "07" ]
```
## Spécifications
| Spécification | État | Commentaires |
| ------------------------------------------------------------------------------------------------------------------------ | ---------------------------- | ------------ |
| {{SpecName('ESDraft', '#sec-regexp-prototype-matchall', 'RegExp.prototype[@@matchAll]')}} | {{Spec2('ESDraft')}} | |
## Compatibilité des navigateurs
{{Compat("javascript.builtins.RegExp.@@matchAll")}}
## Voir aussi
- {{JSxRef("String.prototype.matchAll()")}}
- {{JSxRef("Symbol.matchAll")}}
|