Skip to content

getMandatoryNamedGroup

Reads a mandatory named capture group from a regex match.

Use this for a named group that is ALWAYS present when the pattern matches — not wrapped in an optional (?:…)? / (…)?. Returns the captured string (which may be '' for a zero-width match such as \w*), throwing if the group — or the match’s groups object — is absent, so a pattern change that drops the group fails loudly instead of silently yielding a wrong value.

Prefer this over match.groups?.[name] ?? '': for a mandatory group that fallback is a never-taken (dead) branch that fails a 100%-branch coverage gate, whereas the throw here lives inside ensureNonNullable (already covered), leaving no inline branch at the call site. For a group that may legitimately be absent, use getOptionalNamedGroup and default its null with ?? ….

Import:

import { getMandatoryNamedGroup } from 'obsidian-dev-utils/reg-exp';

Signature:

function getMandatoryNamedGroup(match: RegExpMatchArray, groupName: string): string

Parameters:

ParameterTypeDescription
matchRegExpMatchArrayThe match returned by RegExp.exec, String.matchAll, or String.match.
groupNamestringThe name of the capture group to read.

Returns: string — The captured string.


Links to this page: