Slide 14
Slide 14 text
RegExp Match Indices
• 正規表現パターンマッチングで一致文字の開始&終了位置を取得できる
• 「d」フラグを付けることで可能
• 「indices」プロパティ(配列)のindex:0に配列形式で格納される
• indices:[[開始位置, 終了位置]] のような感じ
• 終了位置は、一致文字列の最終文字のindex+1が入る。(String.slice()の引数と同じ)
• キャプチャグループがある場合、indices[1] 以降に同様の形式で格納される
Node学園 39時限目
// ソースコード
// キャプチャグループなし
const result = /cd/d.exec(‘abcdefabcdefg’);
console.log(result.indices); // [ [2, 4], groups: undefined ]
// キャプチャグループあり
const result = /cd(ef)/d.exec(‘abcdefabcdefg’);
console.log(result.indices); // [ [2, 6], [4, 6], groups: undefined ]