JavaScript

RegExp.prototype.hasIndices プロパティ

 編集:2022.08.22 by T.A.

その RegExpで正規表現の "d" フラグが使用されたか否かを示します。

hasIndices はそれぞれの正規表現インスタンスの読み取り専用プロパティです。

構文
RegExp.hasIndices
RegExp RegExp。またはリテラル表記。
解説

Boolean。"d" フラグが使用された場合は true 、それ以外の場合は false になります。

"d" フラグは正規表現による一致の結果に、各キャプチャ グループの部分文字列の開始と終了の位置を含めることを示します。

const regex1 = new RegExp('foo', 'gd');
D=regex1.hasIndices; //=[boolean]:true
const str1 = 'foo bar foo';
D=regex1.exec(str1); //=[object]:foo
	//0=[string]:foo
	//index=[number]:0
	//input=[string]:foo bar foo
	//groups=[undefined]:undefined
	//indices=[object]:0,3。(開始位置と終了位置)
D=regex1.exec(str1); //=[object]:foo
	//0=[string]:foo
	//index=[number]:8
	//input=[string]:foo bar foo
	//groups=[undefined]:undefined
	//indices=[object]:8,11。(開始位置と終了位置)
D=regex1.exec(str1); //=[object]:null

const regex2 = new RegExp('foo');
D=regex2.hasIndices; //=[boolean]:false
const str2 = 'foo bar foo';
D=regex2.exec(str2); //=[object]:foo
	//0=[string]:foo
	//index=[number]:0
	//input=[string]:foo bar foo
	//groups=[undefined]:undefined

テスト