interface MatchResult<Names extends string = string> { named: Record<Names, string | undefined>;
unnamed: string[];
match: string;
start: number;
end: number;}
export function* matchAll<Names extends string = string>( content: string, regex: RegExp,): Generator<MatchResult<Names>, void, unknown> { for (const contentMatch of content.matchAll(regex)) { yield getResult(contentMatch); }}
export function matchOne<Names extends string = string>( content: string, regex: RegExp,): MatchResult<Names> | undefined { const contentMatch = content.match(regex);
if (!contentMatch) { return; }
return getResult(contentMatch);}
function getResult<Named extends string = string>( value: RegExpMatchArray,): MatchResult<Named> { const [match = "", ...unnamed] = value; const start = value.index ?? 0; const end = start + match.length; const named = value.groups ?? Object.create(null);
return { match, unnamed, start, end, named };}