JavaScript RegExp lastIndex
Syntax & Examples

RegExp.lastIndex property

The lastIndex property of the RegExp object in JavaScript specifies the index at which to start the next match. It is read/write, and its value changes each time the regular expression successfully matches.


Syntax of RegExp.lastIndex

The syntax of RegExp.lastIndex property is:

lastIndex

This lastIndex property of RegExp the index at which to start the next match. It is an integer indicating the 0-based index in the string at which to start the search.

Return Type

RegExp.lastIndex returns value of type Number.



✐ Examples

1 Using lastIndex with a global regular expression

In JavaScript, we can use the lastIndex property with a global regular expression to find successive matches in a string.

For example,

  1. We create a RegExp object regex with the global pattern /\d+/g to match one or more digits.
  2. We use the exec() method to find the first match in the string '123 456 789' and log the result and lastIndex.
  3. We call exec() again to find the next match and log the result and lastIndex again.

JavaScript Program

const regex = /\d+/g;
let result = regex.exec('123 456 789');
console.log(result); // [ '123', index: 0, input: '123 456 789', groups: undefined ]
console.log(regex.lastIndex); // 3
result = regex.exec('123 456 789');
console.log(result); // [ '456', index: 4, input: '123 456 789', groups: undefined ]
console.log(regex.lastIndex); // 7

Output

[ '123', index: 0, input: '123 456 789', groups: undefined ]
3
[ '456', index: 4, input: '123 456 789', groups: undefined ]
7

2 Resetting lastIndex manually

In JavaScript, we can manually reset the lastIndex property to control where the next search starts.

For example,

  1. We create a RegExp object regex with the global pattern /\d+/g.
  2. We use the exec() method to find the first match in the string '123 456 789'.
  3. We manually set regex.lastIndex to 0.
  4. We call exec() again to find the first match from the beginning of the string.

JavaScript Program

const regex = /\d+/g;
let result = regex.exec('123 456 789');
console.log(result); // [ '123', index: 0, input: '123 456 789', groups: undefined ]
regex.lastIndex = 0;
result = regex.exec('123 456 789');
console.log(result); // [ '123', index: 0, input: '123 456 789', groups: undefined ]

Output

[ '123', index: 0, input: '123 456 789', groups: undefined ]
[ '123', index: 0, input: '123 456 789', groups: undefined ]

3 Using lastIndex with non-global regular expressions

In JavaScript, the lastIndex property does not affect non-global regular expressions.

For example,

  1. We create a RegExp object regex without the global flag, /\d+/.
  2. We set regex.lastIndex to 5.
  3. We use the exec() method to search for the pattern in the string '123 456 789'.
  4. We log the result and lastIndex.

JavaScript Program

const regex = /\d+/;
regex.lastIndex = 5;
const result = regex.exec('123 456 789');
console.log(result); // [ '123', index: 0, input: '123 456 789', groups: undefined ]
console.log(regex.lastIndex); // 0

Output

[ '123', index: 0, input: '123 456 789', groups: undefined ]
0

Summary

In this JavaScript tutorial, we learned about lastIndex property of RegExp: the syntax and few working examples with output and detailed explanation for each example.