Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The lastIndex data property of a RegExp instance specifies the index at which to start the next match.
A non-negative integer.
| Writable | yes |
| Enumerable | no |
| Configurable | no |
This property is set only if the regular expression instance used the g flag to indicate a global search, or the y flag to indicate a sticky search. The following rules apply when exec() is called on a given input:
Other regex-related methods, such as RegExp.prototype.test(), String.prototype.match(), String.prototype.replace(), etc., call exec() under the hood, so they have different effects on lastIndex. See their respective pages for details.
Consider the following sequence of statements:
Matches the empty string.
Returns ["hi", "hi"] with lastIndex equal to 2.
Returns ["", undefined], an empty array whose zeroth element is the match string. In this case, the empty string because lastIndex was 2 (and still is 2) and hi has length 2.
The lastIndex property is writable. You can set it to make the regex start its next search at a given index.
The y flag almost always requires setting lastIndex. It always matches strictly at lastIndex and does not attempt any later positions. This is usually useful for writing parsers, when you want to match tokens only at the current position.
The g flag also benefits from setting lastIndex. One common use case is when the string is modified in the middle of a global search. In this case, we may miss a particular match if the string is shortened. We can avoid this by rewinding lastIndex.
Try deleting the mdLinkPattern.lastIndex += resolvedLink.length - originalLink.length line and running the second example. You will find that the second link is not replaced correctly, because the lastIndex is already past the link's index after the string is shortened.
Warning: This example is for demonstration only. To deal with Markdown, you should probably use a parsing library instead of regex.
You can optimize searching by setting lastIndex to a point where previous possible occurrences can be ignored. For example, instead of this:
Consider this:
This is potentially more performant because we avoid string slicing.
The side effects caused by exec() can be confusing, especially if the input is different for each exec().
This is even more confusing when you are hand-modifying lastIndex. To contain the side effects, remember to reset lastIndex after each input is completely processed.
With some abstraction, you can require lastIndex to be set to a particular value before each exec() call.
| ECMAScript® 2027 Language Specification # sec-properties-of-regexp-instances |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 20, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.