npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

ts-regexp

v0.2.1

Published

A RegExp wrapper providing stronger type safety.

Readme

ts-regexp

npm version bundle size npm monthly downloads typescript license: MIT github stars

A RegExp wrapper providing stronger type safety.

const groups1 = new RegExp('^(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})$', 'g').exec('9999-12-31')!.groups;
//      ⤴ '{ [key: string]: string; } | undefined' 🤮
const groups2 = typedRegExp('^(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})$', 'g').exec('9999-12-31')!.groups;
//      ⤴ '{ year: string, month: string, day: string }' 🥰

👉 Try it in the TypeScript Playground

🚀 Setup

  1. Install ts-regexp
# Using npm
npm install ts-regexp

# Using yarn
yarn add ts-regexp

# Using pnpm
pnpm add ts-regexp
  1. Import typedRegExp:
import { typedRegExp } from 'ts-regexp';

🧩 Usage

Runtime wrapper

Import and use typedRegExp just like the native RegExp constructor:

import { typedRegExp } from 'ts-regexp';

const datePattern = typedRegExp('(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})');
const emailPattern = typedRegExp('^(?<local>[a-z0-9._%+-]+)@(?<domain>[a-z0-9.-]+\.[a-z]{2,})$', 'i');

The function signature is:

typedRegExp(pattern: string, flags?: string)

Note: typedRegExp returns a plain object, not a RegExp instance.

Standard RegExp Methods

All standard RegExp methods work exactly as expected, but with equivalent or improved typing:

const pattern = typedRegExp('(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})', 'gid');

// Standard methods
pattern.exec('1970-01-01')!.groups;  // { year: string; month: string; day: string; }
pattern.test('1970-01-01');  // boolean

// Access RegExp properties
pattern.source;  // "(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})"
pattern.flags;   // "dgi"
pattern.global;  // true
pattern.sticky;  // false
//  ...

Regex-first Methods

Each RegExp-related string.prototype method is available as ${MethodName}In with equivalent or improved typing:

const pattern = typedRegExp('(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})');
const string = '1976-11-21';

// Instead of: string.match(pattern)
const match = pattern.matchIn(string); // typed match

// Instead of: string.replace(pattern, replacement)
const formatted1 = pattern.replaceIn(string, '$<day>/$<month>/$<year>');
const formatted2 = pattern.replaceIn(string, (match, year, month, day, offset, string, groups) => `${groups.day}/${groups.month}/${groups.year}`); // typed arguments

// Other inversed methods
pattern.searchIn(string);    // like string.search(pattern)
pattern.splitIn(string);     // like string.split(pattern)

Global Flag Methods

When using the global (g) flag, additional methods become available:

const pattern = typedRegExp('\\d', 'g');

// Only available with 'g' flag
pattern.matchAllIn('1973-12-08');     // like string.matchAll(pattern)
pattern.replaceAllIn('123-456', '#'); // like string.replaceAll(pattern, replacement)

Fallback

If you need access to the underlying RegExp instance:

const pattern = typedRegExp('\\d+');
const nativeRegExp = pattern.regExp; // Regular RegExp instance

Types

ts-regexp exposes some useful parsing types:

Parse

type X = Parse<'(?<a>0)|(?<b>1)'>;
/*
type X = {
    captures: [string, string, undefined];
    namedCaptures: {
        a: string;
        b: undefined;
    };
} | {
    captures: [string, undefined, string];
    namedCaptures: {
        b: string;
        a: undefined;
    };
}
*/

ParseCaptures

type X = ParseCaptures<'(?<a>0)|(?<b>1)'>;
/*
type X = [string, string, undefined] | [string, undefined, string]
*/

ParseNamedCaptures

type X = ParseNamedCaptures<'(?<a>0)|(?<b>1)'>;
/*
type X = {
    a: string;
    b: undefined;
} | {
    b: string;
    a: undefined;
}
*/

✨ Features

  • Strictly typed named & unnamed capture groups
  • Supports contextual awareness
  • Parses:
    • different group types (non-capturing, lookarounds, named captures, etc.)
    • nested groups
    • alternation
    • character classes and escaped characters
  • Infers group optionality from quantifiers (?, *, {n,m})
  • Validates flags
  • Supports dynamic (non-literal) pattern + flag inputs

Alternatives

  • arkregex - A drop-in replacement for new RegExp that, in addition to this library, offers strict literal group typings, readable error messages with powerful validation and best practices suggestions. Maintained by David Blass (creator of ArkType) and actively updated.
  • magic-regexp - A compiled-away, type-safe alternative with a fluent, chainable API that makes regex patterns more readable and maintainable. Instead of cryptic regex syntax, you write exactly('foo').or('bar') which compiles to native RegExp at build time with no runtime cost. Part of the unjs ecosystem.