A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/hasIndices below:

RegExp.prototype.hasIndices - JavaScript | MDN

RegExp.prototype.hasIndices

Baseline Widely available

hasIndices は RegExp インスタンスのプロパティで、その正規表現で d フラグが使用されたかどうかを示します。

試してみましょう
const regex1 = new RegExp("foo", "d");

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

const regex2 = new RegExp("bar");

console.log(regex2.hasIndices);
// Expected output: false
解説

RegExp.prototype.hasIndices の値は d フラグが使用されている場合に true となり、そうでない場合は false となります。d フラグは、正規表現の照合結果に各キャプチャグループの部分文字列の開始と終了のインデックスを含めることを示します。これは正規表現の解釈や照合の動作を変更するものではなく、照合結果に追加情報を与えるだけです。

このフラグは、主に exec() の返値に影響します。d フラグが存在する場合、exec() によって返される配列は、exec() メソッドの返値に記述されているように、追加の indices プロパティを持ちます。他のすべての正規表現関連のメソッド(String.prototype.match() など)は、内部的に exec() を呼び出すので、正規表現に d フラグがある場合、インデックスも返します。

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

例

グループと後方参照 > グループと一致結果の添字の使用に詳しい使用例があります。

hasIndices の使用
const str1 = "foo bar foo";

const regex1 = /foo/dg;

console.log(regex1.hasIndices); // true

console.log(regex1.exec(str1).indices[0]); // [0, 3]
console.log(regex1.exec(str1).indices[0]); // [8, 11]

const str2 = "foo bar foo";

const regex2 = /foo/;

console.log(regex2.hasIndices); // false

console.log(regex2.exec(str2).indices); // undefined
仕様書 ブラウザーの互換性 関連情報

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4