/d (hasIndices)
What is /d (hasIndices)?
/d (hasIndices): Include match start/end indices for the full match and each capture group in the result. The d flag in JavaScript regular expressions adds the start and end positions of every match and capture group to the result. You find them in match.indices. Without it you only get the matched text, and you have to work out the positions yourself.
Frequently asked questions about /d (hasIndices)
How is /d (hasIndices) used in practice?
This is useful when you need to highlight or replace text in the original string. A syntax highlighter, a search tool that marks results, or an editor plugin all need to know exactly where each group sits.
What should you keep in mind with /d (hasIndices)?
It was added in ES2022, so it works in current browsers and Node versions. Calculating positions with indexOf after the fact often goes wrong when the same text appears twice. The d flag gives you the real positions from the regex engine itself. It does cost a little extra work, so leave it off when you only need the text.
At a glance
| Category | Regex flags |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |