From 39f2114f9797eb51994966c6bb8ff1814c9a4da8 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 12:36:08 +0100 Subject: unslug fr: move --- .../global_objects/regexp/@@match/index.html | 119 +++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 files/fr/web/javascript/reference/global_objects/regexp/@@match/index.html (limited to 'files/fr/web/javascript/reference/global_objects/regexp/@@match') diff --git a/files/fr/web/javascript/reference/global_objects/regexp/@@match/index.html b/files/fr/web/javascript/reference/global_objects/regexp/@@match/index.html new file mode 100644 index 0000000000..7adea1beff --- /dev/null +++ b/files/fr/web/javascript/reference/global_objects/regexp/@@match/index.html @@ -0,0 +1,119 @@ +--- +title: 'RegExp.prototype[@@match]()' +slug: Web/JavaScript/Reference/Objets_globaux/RegExp/@@match +tags: + - Expressions rationnelles + - JavaScript + - Méthode + - Prototype + - Reference + - RegExp +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@match +--- +
{{JSRef}}
+ +

La méthode [@@match]() permet de récupérer les correspondances obtenues lorsqu'on teste une chaîne de caractères par rapport à une expression rationnelle (regexp).

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

Syntaxe

+ +
regexp[Symbol.match](str)
+ +

Paramètres

+ +
+
str
+
La chaîne de caractères ({{jsxref("String")}}) sur laquelle on veut chercher des correspondances.
+
+ +

Valeur de retour

+ +

Un tableau ({{jsxref("Array")}}) qui contient les résultats des correspondances et les groupes capturés grâce aux parenthèse. S'il n'y a aucune correspondance, ce sera {{jsxref("null")}}.

+ +

Description

+ +

Cette méthode est appelée de façon interne lorsqu'on utilise {{jsxref("String.prototype.match()")}}. Ainsi, les deux exemples qui suivent sont équivalents et le second est la version interne du premier :

+ +
'abc'.match(/a/);
+
+/a/[Symbol.match]('abc');
+ +

Cette méthode existe afin de permettre d'adapter le comportement de la recherche des correspondances pour les sous-classes de RegExp.

+ +

Exemples

+ +

Appel direct

+ +

Cette méthode peut être utilisée comme {{jsxref("String.prototype.match()")}} mais avec un objet this différent et un ordre des paramètres également différent.

+ +
var re = /[0-9]+/g;
+var str = '2016-01-02';
+var résultat = re[Symbol.match](str);
+console.log(résultat);  // ["2016", "01", "02"]
+
+ +

Utilisation de @@match avec une sous-classe

+ +

Les sous-classes de {{jsxref("RegExp")}} peuvent surcharger la méthode [@@match]() afin de modifier le comportement.

+ +
class MaRegExp extends RegExp {
+  [Symbol.match](str) {
+    var résultat = RegExp.prototype[Symbol.match].call(this, str);
+    if (!résultat) return null;
+    return {
+      group(n) {
+        return résultat[n];
+      }
+    };
+  }
+}
+
+var re = new MaRegExp('([0-9]+)-([0-9]+)-([0-9]+)');
+var str = '2016-01-02';
+var résultat = str.match(re); // String.prototype.match appelle re[@@match].
+console.log(résultat.group(1)); // 2016
+console.log(résultat.group(2)); // 01
+console.log(résultat.group(3)); // 02
+
+ +

Spécifications

+ + + + + + + + + + + + + + + + + + + +
SpécificationÉtatCommentaires
{{SpecName('ES6', '#sec-regexp.prototype-@@match', 'RegExp.prototype[@@match]')}}{{Spec2('ES6')}}Définition initiale.
{{SpecName('ESDraft', '#sec-regexp.prototype-@@match', 'RegExp.prototype[@@match]')}}{{Spec2('ESDraft')}} 
+ +

Compatibilité des navigateurs

+ + + +

{{Compat("javascript.builtins.RegExp.@@match")}}

+ +

Voir aussi

+ + -- cgit v1.2.3-54-g00ecf