DELAPARRA
Index
Color
NL
Category: Regex flags

/d (hasIndices)

Explained by Updated

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.

Definition source Coders Lingo, CC BY 4.0

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

CategoryRegex flags
Explained by
Updated

More Regex flags terms

All terms starting with a number or symbol

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.