JS: Regex Flag d (hasIndices)

By Xah Lee. Date: .

(new in ECMAScript 2022)

// with flag d
// you get a new property named indices in the result.
console.log(
 /i/d.exec("Alice was"),
);

/*
[
  "i",
  index: 2,
  input: "Alice was",
  groups: undefined,
  indices: [ [ 2, 3 ], groups: undefined ]
]
*/

// s------------------------------

// without flag d
console.log(
 /i/.exec("Alice was"),
);
// [ "i", index: 2, input: "Alice was", groups: undefined ]

RegExp.prototype.hasIndices

property RegExp.prototype.hasIndices value is true if flag d is set.

const x = /i/d;
console.assert(x.hasIndices === true);