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 2015年7月.
global は RegExp インスタンスのプロパティで、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® 2026 Language Specification> # sec-get-regexp.prototype.global> | 
ブラウザーの互換性
Loading…