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:
lastIndexThis 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,
- We create a RegExp object regexwith the global pattern/\d+/gto match one or more digits.
- We use the exec()method to find the first match in the string'123 456 789'and log the result andlastIndex.
- We call exec()again to find the next match and log the result andlastIndexagain.
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); // 7Output
[ '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,
- We create a RegExp object regexwith the global pattern/\d+/g.
- We use the exec()method to find the first match in the string'123 456 789'.
- We manually set regex.lastIndexto 0.
- 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,
- We create a RegExp object regexwithout the global flag,/\d+/.
- We set regex.lastIndexto 5.
- We use the exec()method to search for the pattern in the string'123 456 789'.
- 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); // 0Output
[ '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.
