RegExp.prototype.hasIndices

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

試してみましょう

RegExp.prototype.hasIndices のプロパティ属性
書込可能 不可
列挙可能 不可
設定可能

解説

hasIndices の値は論理型であり、 "d" フラグが使用された場合は true になります。それ以外の場合は false になります。 "d" フラグは正規表現による一致の結果に、各キャプチャグループの部分文字列の開始と終了の位置を含めることを示します。

このプロパティを直接変更することはできません。

hasIndices の使用

const str1 = 'foo bar foo';

const regex1 = new RegExp('foo', 'gd');

console.log(regex1.hasIndices); // 出力: true

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

const str2 = 'foo bar foo';

const regex2 = new RegExp('foo');

console.log(regex2.hasIndices); // 出力: false

console.log(regex2.exec(str2).indices); // 出力: undefined

仕様書

Specification
ECMAScript Language Specification
# sec-get-regexp.prototype.hasIndices

ブラウザーの互換性

BCD tables only load in the browser

関連情報