JavaScript Reference ยท RegExp
RegExp lastIndex
Reference for RegExp lastIndex: how the g and y flags make test() and exec() stateful, why a regex gives alternating results, and how to reset it.
lastIndex is a read/write property of every RegExp object. It holds the string index where the next search will start. It only matters when the regular expression has the g (global) or y (sticky) flag. For other regular expressions it is ignored: test() and exec() always start at index 0 and never change it.
This hidden state is the source of one of the most common regex bugs: calling test() twice on the same global regex with the same string can give true and then false.
const re = /a/g; re.lastIndex; // 0 at the start re.test("banana"); // true, and lastIndex becomes 2 re.lastIndex; // 2
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
regex.lastIndex // read regex.lastIndex = 0 // write (an integer index)
| Situation | What happens to lastIndex |
|---|---|
exec() or test() finds a match (g or y set) |
Set to the index just after the match (the match's end). |
exec() or test() fails (g or y set) |
Reset to 0. |
lastIndex is greater than the string length |
The search fails and lastIndex becomes 0. |
Neither g nor y is set |
Not read and not changed by exec() and test(). |
Both g and y are set |
The match must start exactly at lastIndex, as with y alone. |
Usage notes
- Reset it when you reuse a global regex for a new string:
re.lastIndex = 0. Or create the regex where you use it; a regex literal evaluated inside a function creates a new object each time, so the state does not leak between calls. - Do not use
gfor plain checks.test()withgis rarely what you want. Without the flag, the result is the same every time. - A zero-length match does not advance by itself. In a manual
exec()loop with a pattern that can match the empty string (such as/x*/g), you must bumplastIndexyourself or the loop never ends. The built-inmatchAll(),replace()andsplit()handle this for you. String.prototype.match(),matchAll(),replace()andsplit()with a global regex start from the beginning regardless of the currentlastIndex:match()andreplace()reset it to 0 first and leave it at 0 afterwards, andmatchAll()works on a copy. Onlytest(),exec()and some internal calls use the current value.- Sticky matching is useful for tokenizers: set
lastIndexto the current position, callexec()with ayregex, and a match must begin right there with no skipping. Use one sticky regex per token type. lastIndexis in UTF-16 code units, like string indexes. With theuflag, setting it in the middle of a surrogate pair can behave unexpectedly.- A frozen regex cannot be used statefully. Freezing makes
lastIndexread-only, soexec()andtest()on agoryregex throw aTypeErrorwhen they try to update it. - Shared regexes are shared state. A module-level global regex used from several callers, or from async code, can give different answers depending on call order.
Browser support
lastIndex and the g flag behave the same in every JavaScript engine. The y flag is supported by all current browsers.