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 🙏

© 2026 – Pkg Stats / Ryan Hefner

literate-regex

v0.6.11

Published

Write readable (literate) regex sources with # comments, then normalize them to JS RegExp.source at the type level.

Downloads

637

Readme

LICENSE npm version npm bundle size npm npms.io

literate-regex

A literate, typed JavaScript regex toolkit — powered by TypeScript.

Write regex like a human. Ship it like a machine.

✨ Motivation

Regular expressions are absurdly powerful — a tiny automaton you can carry in your pocket.
But once a regex grows beyond “a few tokens and a prayer”, it becomes:

  • hard to read
  • easy to break
  • painful to maintain

This library exists to keep regexes literate.

Write a multi-line, commented, PCRE-style regex source (with # notes), then normalize it into a compact JavaScript RegExp.source while preserving the normalized source as a TypeScript string literal type.

That gives you two superpowers:

  1. Human-friendly editing (readable formatting + comments)
  2. Machine-friendly safety (typed, normalized sources that flow through your codebase)

In short: fewer regex jump-scares, more confidence.

Before

const re = /^(?:\s*\/\*\*\s+|\s+\*?\s+)(?:(?=@(...))|...)/gm;

After

const RE_SOURCE = `
/^         # start
(?: ... )  # jsdoc start
...        # more notes
/gm` as const;

const re = compilePCREStyleRegExpLiteral(RE_SOURCE);

✨ Features

  • PCRE-ish style regex source:
    • multi-line formatting
    • # ... line comments
    • \# escape for literal #
  • Type-level normalization:
    • derive normalized JS RegExp.source as a string literal type
  • Optional global augmentation:
    • opt-in only (import "literate-regex/global")
  • Designed to reduce TypeScript instantiation pain:
    • line-oriented normalization (helps avoid ts(2589) compared to naive full-string scanning)

📦 Install

npm i literate-regex
# or
pnpm add literate-regex
# or
yarn add literate-regex

🚀 Quick Start

import { PCREStyleToJsRegExpSource } from "literate-regex";

// Only those who want to expand globally
import "literate-regex/global";

🧠 Type-level normalization

1) Write a readable PCRE-style source

  • # starts a line comment (unless escaped)
  • \# is kept as a literal #
  • whitespace characters are stripped during normalization
import type { PCREStyleToJsRegExpSource } from "./literate-regex";

// sample 1
const RE_SOURCE = `
^           # start
(?:\\#\\w+) # literal "#"
\\s+        # whitespace
` as const;

// type JsSource = "^(?:#\\w+)\\s+" 
type JsSource = PCREStyleToJsRegExpSource<typeof RE_SOURCE>;

Tip: You must use as const to preserve the source as a string literal type.


🔧 Runtime normalization (optional)

PCREStyleToJsRegExpSource<...> is purely type-level. If you also normalize at runtime, mirror the same rules:

import { normalizePCREStyleSource } from "literate-regex";
// import type { PCREStyleToJsRegExpSource } from "literate-regex";

// sample 2
const src = `
^        # start
\\#\\w+  # literal
` as const;

// '^#\\w+'
// const normalized: "^#\\w+"
const normalized = normalizePCREStyleSource(src);

🔧 Runtime creation Compile PCRE Style RegExpLiteral

import {
  TypedRegExp,
  // normalizePCREStyleSource,
  compilePCREStyleRegExpLiteral,
} from "literate-regex";
import type {
  RegExpLiteralParts,
  PCREStyleToJsRegExpSource,
  RegExpExecArrayFixedPretty,
  ReplacerFunctionSignature,
} from "literate-regex";

//
// sample of compilePCREStyleRegExpLiteral
//
const pcreStyledRegex = `/
(\\(\\?\#[\\s\\S]*?(?<!\\\\)\\)(?=\\s*$|.))         # multi line comment
|
(?:^(?:\\s+|))?(?<![\\\\])(\\#(?:\\s|[\\s\\S])*?$)  # single line comment
|
(?<regexFragment>
  (?:^\\s+)?(?:[^\\s]+)
)+                                                  # regex flagment
|
([\\r|\\r\\n|\\n]+|[\\x20\\t]+(?=$)?)               # whitespaces
/gm`;

const jsRegex = compilePCREStyleRegExpLiteral(pcreStyledRegex);

type TPcreStyledRegex = typeof pcreStyledRegex;
type TJsRegexSource = PCREStyleToJsRegExpSource<TPcreStyledRegex>;
type TJsRegexLiteralParts = RegExpLiteralParts<TJsRegexSource>;

type TJsRegexExecArray = RegExpExecArrayFixedPretty<
  TypedRegExp<TJsRegexLiteralParts["pattern"]>
>;
type TJsRegexStringReplacer = ReplacerFunctionSignature<
  TypedRegExp<TJsRegexLiteralParts["pattern"]>
>;
let m = jsRegex.exec(pcreStyledRegex);
type Test0 = TJsRegexExecArray extends typeof m ? true : false;
type Test1 = typeof m extends TJsRegexExecArray ? true : false;

const replacer: TJsRegexStringReplacer = (...args) => "";
pcreStyledRegex.replace(jsRegex, replacer);
pcreStyledRegex.replace(jsRegex, "");

🌍 Global augmentation (opt-in)

This package provides an optional global augmentation entry:

import "literate-regex/global";

This is intentionally opt-in to avoid unexpected type pollution across projects.


⚠️ Notes & limitations

  • This is not a full PCRE parser. It focuses on:

    • line comments (# ...)
    • escaping \#
    • whitespace stripping
  • Very large type-level inputs may still hit TS limits depending on your environment. If that happens, split your regex source into smaller pieces.


📚 References

This library’s whitespace set is based on the ECMAScript definition used by RegExp \s (WhiteSpace ∪ LineTerminator).

  • ECMA-262: White Space (Table 33) https://tc39.es/ecma262/#sec-white-space
  • ECMA-262: Line Terminators (Table 34) https://tc39.es/ecma262/#sec-line-terminators
  • MDN: RegExp character classes (\s equivalence) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes

📜 License

Released under the Apache-2.0 License.
See LICENSE for details.