getOptionalNamedGroup
Reads an optional named capture group from a regex match, distinguishing an absent group from a present empty one.
Returns the captured string when the group participated in the match — which may be '' for a zero-width match such as \w* — or null when the group, or the match’s groups object, is absent (e.g. an optional (?:…)? group that did not match). Because null is returned only for a genuinely-absent group, a caller’s ?? <default> is a live branch rather than the dead one that match.groups?.[name] ?? '' leaves for a mandatory group. For a group that must always be present, use getMandatoryNamedGroup.
Import:
import { getOptionalNamedGroup } from 'obsidian-dev-utils/reg-exp';Signature:
function getOptionalNamedGroup(match: RegExpMatchArray, groupName: string): string | nullParameters:
| Parameter | Type | Description |
|---|---|---|
match | RegExpMatchArray | The match returned by RegExp.exec, String.matchAll, or String.match. |
groupName | string | The name of the capture group to read. |
Returns: string | null — The captured string, or null when the group is absent.
Links to this page: