1.0.4 • Published 1 year ago

typed-regexp v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

typed-regexp

A typescript package that strongly restricts types of regular expressions.

Examples

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}`
}