Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
const re = /a/g;
re.lastIndex;          // 0 at the start
re.test("banana");     // true, and lastIndex becomes 2
re.lastIndex;          // 2

Try it

JS Watching lastIndex move

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Loops, resets and sticky matching

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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 g for plain checks. test() with g is 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 bump lastIndex yourself or the loop never ends. The built-in matchAll(), replace() and split() handle this for you.
  • String.prototype.match(), matchAll(), replace() and split() with a global regex start from the beginning regardless of the current lastIndex: match() and replace() reset it to 0 first and leave it at 0 afterwards, and matchAll() works on a copy. Only test(), exec() and some internal calls use the current value.
  • Sticky matching is useful for tokenizers: set lastIndex to the current position, call exec() with a y regex, and a match must begin right there with no skipping. Use one sticky regex per token type.
  • lastIndex is in UTF-16 code units, like string indexes. With the u flag, setting it in the middle of a surrogate pair can behave unexpectedly.
  • A frozen regex cannot be used statefully. Freezing makes lastIndex read-only, so exec() and test() on a g or y regex throw a TypeError when 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.