--- title: RegExp.prototype.flags slug: Web/JavaScript/Reference/Global_Objects/RegExp/flags tags: - ECMAScript 2015 - JavaScript - Property - Prototype - Reference - RegExp - Regular Expressions translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/flags ---
{{JSRef}}

flags プロパティは、現在の正規表現オブジェクトのフラグから成る文字列を返します。

{{EmbedInteractiveExample("pages/js/regexp-prototype-flags.html")}}

{{JS_Property_Attributes(0, 0, 1)}}

解説

flags プロパティのフラグはアルファベット順にソートされます(左から右へ 例えば、"gimuy")。

flags の使用

/foo/ig.flags;   // "gi"
/bar/myu.flags;  // "muy"

ポリフィル

if (RegExp.prototype.flags === undefined) {
  Object.defineProperty(RegExp.prototype, 'flags', {
    configurable: true,
    get: function() {
      return this.toString().match(/[gimsuy]*$/)[0];
    }
  });
}

仕様書

仕様書
{{SpecName("ESDraft", "#sec-get-regexp.prototype.flags", "RegExp.prototype.flags")}}

ブラウザーの互換性

{{Compat("javascript.builtins.RegExp.flags")}}

関連情報