JavaScript RegExp hasIndices Property

Beginner
5 min read
Updated: Nov 2026
3 examples
RegExp

Definition

In JavaScript, regex.hasIndices is a read-only boolean. It is true when the RegExp was created with the d flag.

With d, a successful exec() result includes an indices array. Each entry is [start, end] for the full match and for each capture group.

Syntax

Create the regex with d, then read .hasIndices and use exec.

JavaScript
const re = /(\d+)/d;
const m = re.exec("room 42");

console.log(re.hasIndices); // true
console.log(m[0]);          // "42"
console.log(m.indices[0]);  // [5, 7]
CallWhat it does
/(\d+)/dIndices mode. hasIndices is true.
re.exec(str)Returns a match object that includes indices.
m.indices[0]Start and end indexes of the full match.

Remember this at a glance:

Remember
 /(\d+)/.hasIndices     → false
 /(\d+)/d.hasIndices    → true

with d, exec() result has:
  match.indices[0]     → [start, end] of full match
  match.indices[1]     → [start, end] of group 1

related flag page: modifier-d

What You Get

NameDescription
regex.hasIndicesRead-only. true if the d flag was set when the RegExp was created.
d flagSet at creation with /…/d or new RegExp(pattern, "d").
match.indicesArray of [start, end] pairs for the match and groups.

Return Value

ValueDescription
trueThe RegExp has the d (hasIndices) flag.
falseThe RegExp does not have the d flag.

JavaScript Examples

Example 1 — Read hasIndices

See how the d flag sets the property.

JavaScript
const a = /\d+/;
const b = /\d+/d;
const c = new RegExp("\\d+", "d");

console.log(a.hasIndices);
console.log(b.hasIndices);
console.log(c.hasIndices);
console.log(c.flags);
Try It Yourself

How It Works

  1. Without d, hasIndices is false.
  2. With /\d+/d, hasIndices is true.
  3. The constructor with "d" does the same.
  4. flags reports "d".

Example 2 — Read start and end indexes

Use exec and print indices.

JavaScript
const re = /(\d+)/d;
const m = re.exec("id: 17");

console.log(m[0]);
console.log(m.indices[0].join(","));
console.log(m.indices[1].join(","));
Try It Yourself

How It Works

  1. m[0] is the matched text "17".
  2. indices[0] is the full match span: start 4, end 6.
  3. indices[1] is group 1. Here it is the same span.

Example 3 — Slice the original string

Use the indexes to cut text from the input.

JavaScript
const text = "price $12.50";
const re = /\$(\d+)/d;
const m = re.exec(text);
const [start, end] = m.indices[0];

console.log(text.slice(start, end));
console.log(m[1]);
Try It Yourself

How It Works

  1. The full match is $12 from index start to end.
  2. Group 1 is only the digits 12.
  3. Slicing with indices is handy for editors and highlighters.

Frequently Asked Questions

It is true when the regex was created with the d flag. Then exec() results include an indices array of [start, end] pairs.
match.index gives the start of the full match. The d flag also gives end indexes and per-group spans.
They describe the same flag. This page focuses on the .hasIndices property; the d modifier page focuses on writing the flag.
Yes. indices.groups holds [start, end] for each named capture when present.
No. Create a new RegExp with the d flag.
Modern browsers and recent Node.js versions support it. Older engines may not expose indices.

Next: flags

Read every flag on a RegExp as one sorted string.

flags 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