sertonix / typed-regexp

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

Home Page:https://www.npmjs.com/package/typed-regexp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

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

https://www.npmjs.com/package/typed-regexp

License:MIT License


Languages

Language:TypeScript 99.0%Language:JavaScript 1.0%