Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. RegExp
  6. hasIndices

RegExp.prototype.hasIndices

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨September 2021⁩.

ThehasIndices accessor property ofRegExp instances returns whether or not thed flag is used with this regular expression.

Try it

const regex1 = /foo/d;console.log(regex1.hasIndices);// Expected output: trueconst regex2 = /bar/;console.log(regex2.hasIndices);// Expected output: false

Description

RegExp.prototype.hasIndices has the valuetrue if thed flag was used; otherwise,false. Thed flag indicates that the result of a regular expression match should contain the start and end indices of the substrings of each capture group. It does not change the regex's interpretation or matching behavior in any way, but only provides additional information in the matching result.

This flag primarily affects the return value ofexec(). If thed flag is present, the array returned byexec() has an additionalindices property as described in theexec() method'sreturn value. Because all other regex-related methods (such asString.prototype.match()) callexec() internally, they will also return the indices if the regex has thed flag.

The set accessor ofhasIndices isundefined. You cannot change this property directly.

Examples

There's a more detailed usage example atGroups and backreferences > Using groups and match indices.

Using hasIndices

js
const str1 = "foo bar foo";const regex1 = /foo/dg;console.log(regex1.hasIndices); // trueconsole.log(regex1.exec(str1).indices[0]); // [0, 3]console.log(regex1.exec(str1).indices[0]); // [8, 11]const str2 = "foo bar foo";const regex2 = /foo/;console.log(regex2.hasIndices); // falseconsole.log(regex2.exec(str2).indices); // undefined

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-get-regexp.prototype.hasIndices

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp