JavaScript RegExp lastIndex Property

Beginner
5 min read
Updated: Nov 2026
3 examples
RegExp

Definition

In JavaScript, regex.lastIndex is a writable number. It is the index where the next search starts for a global (g) or sticky (y) RegExp.

After a successful exec() or test(), the engine updates lastIndex. If you reuse the same regex, reset it with regex.lastIndex = 0 when you want to start over.

Syntax

Read or set lastIndex on a global or sticky regex.

JavaScript
const re = /\d+/g;
const text = "a1 b2";

console.log(re.exec(text)[0]); // "1"
console.log(re.lastIndex);     // 2
console.log(re.exec(text)[0]); // "2"

re.lastIndex = 0;              // start over
CallWhat it does
regex.lastIndexRead the next search index (number).
regex.lastIndex = 0Restart searching from the beginning.
regex.exec(str)With g or y, updates lastIndex after each call.

Remember this at a glance:

Remember
 /\d+/g.lastIndex     → 0 at first

with g or y:
  each exec()/test() may change lastIndex
  reset: regex.lastIndex = 0

without g/y:
  lastIndex is usually ignored by exec/test

What You Get

NameDescription
regex.lastIndexWritable integer. Next search index for g or y regexes.
DefaultStarts at 0 on a new RegExp.
After failureOften reset to 0 when a search finds no more matches.

Value

ValueDescription
Number ≥ 0Index in the string where the next g/y search begins.

JavaScript Examples

Example 1 — Watch lastIndex change

Call exec twice on a global regex.

JavaScript
const re = /\d+/g;
const text = "x12y34";

console.log(re.exec(text)[0]);
console.log(re.lastIndex);
console.log(re.exec(text)[0]);
console.log(re.lastIndex);
Try It Yourself

How It Works

  1. First match is 12; lastIndex moves to 3.
  2. Second call continues from index 3 and finds 34.
  3. After that match, lastIndex is 6.

Example 2 — Reset before testing again

Avoid the global test() trap.

JavaScript
const re = /cat/g;

console.log(re.test("cat"));
console.log(re.test("cat"));
re.lastIndex = 0;
console.log(re.test("cat"));
Try It Yourself

How It Works

  1. First test finds cat and advances lastIndex.
  2. Second test starts after the match and fails.
  3. Resetting to 0 makes the third test succeed again.

Example 3 — Loop all matches

Use exec until it returns null.

JavaScript
const re = /\w+/g;
const text = "one two three";
let m;
const words = [];

while ((m = re.exec(text)) !== null) {
  words.push(m[0]);
}

console.log(words.join(", "));
console.log(re.lastIndex);
Try It Yourself

How It Works

  1. Each loop step reads the next word via lastIndex.
  2. When nothing remains, exec returns null and the loop stops.
  3. After a failed search, lastIndex is reset to 0.

Frequently Asked Questions

A number that stores where the next search starts for a global (g) or sticky (y) RegExp. You can read and write it.
When the regex has g or y. Methods like exec() and test() use and update it. Without those flags, you can usually ignore it.
On a global regex, the first test advances lastIndex. The second call may start past the match. Reset lastIndex to 0, or omit g for simple checks.
Set regex.lastIndex = 0 before calling exec() or test() again on the same string from the start.
matchAll works with global regexes and manages iteration for you. Still be careful if you reuse the same RegExp object elsewhere.
No. match.index is where the current match started. lastIndex is where the next search will start.

Next: test()

Ask a yes-or-no question with RegExp.test().

test() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful