RegExp.prototype.flags
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since janvier 2020.
La propriété flags renvoie une chaîne de caractères contenant les drapeaux (flags) de l'objet RegExp auquel elle appartient.
Exemple interactif
// Outputs RegExp flags in alphabetical order
console.log(/foo/gi.flags);
// Expected output: "gi"
console.log(/bar/muy.flags);
// Expected output: "muy"
Attributs de RegExp.prototype.flags | |
|---|---|
| Écrivable | Non |
| Énumérable | Non |
| Configurable | Oui |
Description
Les drapeaux de la propriété flags sont rangés par ordre alphabétique de gauche à droite.
Exemples
>Utiliser flags
js
/toto/gi.flags; // "gi"
/truc/muy.flags; // "muy"
Prothèse d'émulation (polyfill)
js
if (RegExp.prototype.flags === undefined) {
Object.defineProperty(RegExp.prototype, "flags", {
configurable: true,
get: function () {
return this.toString().match(/[gimuy]*$/)[0];
},
});
}
Spécifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-get-regexp.prototype.flags> |
Compatibilité des navigateurs
Loading…