「JavaScript」RegExp.lastIndexを使うサンプル

2021年2月17日

書式
regExpObj.lastIndex
lastIndex は正規表現インスタンスの読み書き可能なプロパティで、次の一致を開始する位置を指定します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const regptn= new RegExp( 'abc', 'g' );
const res = 'study abctarkgame, abcsinfo';
regptn.test(res);
console.log("インデックス結果1: "+regptn.lastIndex);
regptn.test("test結果2:"+res);
console.log("インデックス結果2: "+regptn.lastIndex);
const regptn= new RegExp( 'abc', 'g' ); const res = 'study abctarkgame, abcsinfo'; regptn.test(res); console.log("インデックス結果1: "+regptn.lastIndex); regptn.test("test結果2:"+res); console.log("インデックス結果2: "+regptn.lastIndex);
const regptn= new RegExp( 'abc', 'g' );
const res = 'study abctarkgame, abcsinfo';

regptn.test(res);

console.log("インデックス結果1: "+regptn.lastIndex);


regptn.test("test結果2:"+res);

console.log("インデックス結果2: "+regptn.lastIndex);

結果
> “インデックス結果1: 9"
> “インデックス結果2: 17"

JavaScript

Posted by arkgame