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]
Call
What it does
/(\d+)/d
Indices 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
Name
Description
regex.hasIndices
Read-only. true if the d flag was set when the RegExp was created.
d flag
Set at creation with /…/d or new RegExp(pattern, "d").
match.indices
Array of [start, end] pairs for the match and groups.
Return Value
Value
Description
true
The RegExp has the d (hasIndices) flag.
false
The 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);