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 July 2015.

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
ÉcrivableNon
ÉnumérableNon
ConfigurableOui

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® 2025 Language Specification
# sec-get-regexp.prototype.flags

Compatibilité des navigateurs

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
flags

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Voir aussi