A typescript package that strongly restricts types of regular expressions.
As direct dependency: npm i typed-regexp
import { TypedRegExp } from "typed-regexp";
const regexp = new TypedRegExp<[`{${string}`],{name:"a"|"b"}>("(?<name>[ab])({.*)");
const match = "string to match".match(regexp);
if (match) {
const namedGroup = match.groups.name;
// => "a"|"b"
const group = match[1];
// => `{${string}`
}
or as dev dependency: npm i -D typed-regexp
import type { TypedRegExp } from "typed-regexp";
const regexp = /(?<name>[ab])({.*)/ as TypedRegExp<[`{${string}`],{name:"a"|"b"}>;
const match = "string to match".match(regexp);
if (match) {
const namedGroup = match.groups.name;
// => "a"|"b"
const group = match[1];
// => `{${string}`
}