Open In App

JavaScript RegExp lastIndex Property

Last Updated : 05 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The lastIndex property of a JavaScript regular expression object indicates the index at which to start the next match. This property is particularly useful when using methods like exec() or test() in combination with global (g) or sticky (y) flags.

let regex = /test/g;
// Regular expression with 'g' flag
let str = "test test";
regex.exec(str); 
console.log(regex.lastIndex); 
regex.exec(str); 
console.log(regex.lastIndex); 
  • After the first match, lastIndex is updated to the position right after the matched substring.
  • The second match starts from the updated lastIndex.

Syntax:

regex.lastIndex

Key Points

  • Default Value: For a newly created regex object, lastIndex defaults to 0.
  • Read-Write Property: You can manually set lastIndex to control where matching starts.
  • Applicable Flags: lastIndex is only meaningful when using the g (global) or y (sticky) flags.
  • Reset Automatically: If a match is unsuccessful, lastIndex is reset to 0 for non-sticky patterns.

Real-World Examples

1. Iterating Through Matches

let regex = /\d+/g; // Match digits
let str = "123 456 789";
let match;
while ((match = regex.exec(str)) !== null) {
    console.log(`Matched: ${match[0]}, Next search starts at: ${regex.lastIndex}`);
}

The lastIndex property ensures that matches are found sequentially.

2. Using lastIndex with Sticky Regex

let regex = /\d+/y; 
// Sticky regex
let str = "123 456";
regex.lastIndex = 4; 
// Start search from index 4
console.log(regex.exec(str));
console.log(regex.lastIndex);

With the y flag, the regex only matches if the lastIndex position aligns with the match.

3. Manually Resetting lastIndex

let regex = /hello/g;
let str = "hello world hello";

regex.lastIndex = 6; 
// Start searching from index 6
console.log(regex.exec(str)); 
console.log(regex.lastIndex); 

You can manually set lastIndex to skip certain portions of the string.

4. Testing Without Resetting

let regex = /foo/g;
let str = "foo bar foo";

regex.test(str);
 // Finds the first "foo"
console.log(regex.lastIndex);

regex.test(str);
 // Finds the second "foo"
console.log(regex.lastIndex); 

With the g flag, lastIndex is updated after every test() call.

5. Resetting on No Match

let regex = /abc/g;
let str = "abc def";

regex.lastIndex = 4;
 // Start at index 4
console.log(regex.exec(str)); 
console.log(regex.lastIndex);

If no match is found, lastIndex resets to 0.

Why Use the lastIndex Property?

  • Sequential Matching: Enables efficient iteration through all matches in a string.
  • Precise Control: Allows manual adjustment of the matching starting point.
  • Sticky Patterns: Essential for strict position-based matching with the y flag.
  • Dynamic Applications: Useful for parsing structured data or building advanced search functionalities.

Conclusion

The lastIndex property is a powerful feature for working with regular expressions in JavaScript, offering fine-grained control over how and where matching starts. It’s especially useful in scenarios requiring iterative or position-based matching.

Recommended Links:



Next Article

Similar Reads

three90RightbarBannerImg