Skip to content

[Moved to Codeberg] Adds type checking for regular expressions.

License

Notifications You must be signed in to change notification settings

sertonix/typed-regexp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Links