RegExp.lastIndex

lastIndex — Sets or gets the index from which next to start the next match.

Description

RegExp.lastIndex

Return values

integer

Examples

Example #1 – lastIndex example

lastIndex is incremented when performing a test.

regex = /^a/g; console.log(regex.lastIndex, regex.test("abc"), regex.lastIndex); // true, lastIndex was 0 console.log(regex.lastIndex, regex.test("abc"), regex.lastIndex); // false, lastIndex was 1

It can be reset by setting the lastIndex back to beginning.

regex = /^a/g; console.log(regex.lastIndex, regex.test("abc"), regex.lastIndex); // true, lastIndex was 0 regex.lastIndex = 0; console.log(regex.lastIndex, regex.test("abc"), regex.lastIndex); // true, lastIndex was 0

External references