RegExp.prototype.global

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.

globalRegExp インスタンスのプロパティで、g フラグが正規表現で使われているかどうかを返します。

試してみましょう

const regex1 = new RegExp("foo", "g");

console.log(regex1.global);
// Expected output: true

const regex2 = new RegExp("bar", "i");

console.log(regex2.global);
// Expected output: false

解説

RegExp.prototype.global は、g フラグが使用された場合は true、そうでない場合は false になります。g フラグは、正規表現が文字列内のすべての可能なマッチに対してテストされるべきであることを示します。 exec() を呼び出すたびに lastIndex プロパティが更新され、次の exec() の呼び出しが次の文字から始まるようになります。

String.prototype.matchAll()String.prototype.replaceAll() のようないくつかのメソッドは、引数が正規表現である場合、それがグローバルであることを検証します。正規表現の [Symbol.match]() および [Symbol.replace]()String.prototype.match()String.prototype.replace() によって呼び出されます)も、正規表現がグローバルである場合に異なる動作をします。

global の設定アクセサーは undefined です。このプロパティを直接変更することはできません。

global の使用

js
const regex = /foo/g;
console.log(regex.global); // true

const str = "fooexamplefoo";
const str1 = str.replace(regex, "");
console.log(str1); // example

const regex1 = /foo/;
const str2 = str.replace(regex1, "");
console.log(str2); // examplefoo

仕様書

Specification
ECMAScript® 2025 Language Specification
# sec-get-regexp.prototype.global

ブラウザーの互換性

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
global
Prototype accessor property (ES2015)

Legend

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

Full support
Full support

関連情報