Favorites

JavaScript RegExp.prototype.lastIndex Property

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
RegExp.prototype.lastIndex
JavaScript RegExp Instance propertyThis property is supported by all modern browsers.
Updated 28th November 2026

The lastIndex property holds the position where a global or sticky regular expression will start its next search.

Description

The lastIndex property of a regular expression with the g or y flag holds the index to start the next match from. exec() and test() update it after each match and reset it to 0 when no match is found. Set it to 0 yourself to restart a search.

It also affects exec() and string methods that use the regex, such as replace() with a global pattern, which resets it to 0 when done. A common bug is reusing a global regex stored in a variable or constant to test several strings: each test starts where the last one ended, so some matches are missed.

The simplest fix is to leave out the g flag when you only need to test for a match, or set lastIndex = 0 before each use. With the sticky y flag, a match must start exactly at lastIndex, which is useful for writing tokenizers.

Syntax

regexp.lastIndex

Value

A non-negative integer: the index at which the next match will start.

Example

const re = /o/g;
const s = 'foo boo';
re.test(s);
console.log(re.lastIndex);
re.test(s);
console.log(re.lastIndex);
re.lastIndex = 0;

Browser Support

The following information will show you the current browser support for the RegExp.prototype.lastIndex property. Hover over a browser icon to see the version that first introduced support for this property.

This property is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee