JavaScriptのlastIndexプロパティ
例
let text = "The rain in Spain stays mainly in the plain";
let pattern = /ain/g;
let result = "";
while (pattern.test(text)==true) {
result += "Found at pos " + pattern.lastIndex + "<br>";
}
定義と使用法
lastIndexプロパティは、次の一致を開始するインデックスを指定します。
注:このプロパティは、「g」修飾子が設定されている場合にのみ機能します。
このプロパティは、exec()またはtest()メソッドによって検出された最後の一致の直後の文字位置を指定する整数を返します。
注: exec()とtest()は、一致しない場合、lastIndexを0にリセットします。
ブラウザのサポート
lastIndex
ECMAScript1(ES1)機能です。
ES1(JavaScript 1997)は、すべてのブラウザーで完全にサポートされています。
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
構文
RegExpObject.lastIndex
戻り値
Type | Description |
---|---|
Number | An integer that specifies the character position immediately after the last match found by exec( ) or test( ) methods |