path-to-regexp
Turn path strings like /user/:id into regular expressions for matching, building, and parsing URLs.
Repository Health
Technical Analysis
path-to-regexp is the small TypeScript library that converts Express-style path patterns into JavaScript regular expressions, and back again. It exports five focused functions — parse, pathToRegexp, match, compile, and stringify — that together let you tokenize a path pattern, compile it to a RegExp with capture groups, match incoming strings against it, or build a concrete URL from named parameters.
Originally extracted from Express’s internal routing code, it has since become the de facto path-matching engine embedded inside Express, React Router, Vue Router, and dozens of other routing libraries across the npm ecosystem, while remaining usable standalone for anything that needs ordered, delimiter-based pattern matching.
What You Get
pathToRegexp()— compiles a path pattern (or array of patterns) into a RegExp plus akeysarray describing each capture groupmatch()— a higher-level matcher that returns a function taking a string and returning{ path, params }orfalsecompile()— the reverse operation: turns a pattern into a function that builds a concrete path string from a params objectparse()/stringify()— tokenize a pattern into aTokenDatastructure and serialize it back, for programmatic pattern construction- Named parameters (
:name), wildcards (*name), and optional groups ({...}) with a syntax redesigned in v6+/v8 to eliminate ReDoS-prone constructs
Common Use Cases
- Implementing the route-matching layer of a custom HTTP server or framework
- Matching or generating URLs in a client-side single-page-app router
- Validating and reverse-generating paths from a shared route-definition object (e.g. typed API clients)
- Building custom middleware that needs to test a request path against a set of patterns without pulling in a full router
Under The Hood
Architecture — The entire library lives in one file, src/index.ts (~670 lines), organized as a small pipeline: parse() walks the pattern character-by-character (via consumeUntil) into a flat Token[] of text/param/wildcard/group nodes wrapped in a TokenData class; pathToRegexp() and compile() both consume that same TokenData rather than re-parsing, so parse/match/build all share one token model. match() is implemented as a thin wrapper around pathToRegexp() that additionally decodes captured params using the token keys array, and compile() walks the tokens in the opposite direction (tokensToFunction) to stitch a params object back into a string, collecting missing parameter names for a TypeError instead of emitting a malformed path.
Tech Stack — Pure TypeScript with zero runtime dependencies, targeting ES module output (dist/index.js, dist/index.d.ts) built via @borderless/ts-scripts. Tests run on Vitest (vitest.config.mts), with @vitest/coverage-v8 for coverage and size-limit enforcing a 2 kB budget on the compiled bundle as part of npm test — a strong signal this library optimizes hard for being embedded inside other packages’ dependency trees.
Code Quality — src/index.spec.ts (24 it() blocks) covers the public API surface directly, while src/cases.spec.ts is a large (~48 KB) table-driven fixture file of pattern/input/expected-match triples run through a shared test harness — the more common pattern for exhaustively covering a parser’s edge cases than dozens of individual it() calls. src/redos.spec.ts specifically fuzzes patterns for catastrophic backtracking using the recheck dev dependency, reflecting the project’s post-v6 rewrite to eliminate ReDoS vulnerabilities that affected earlier majors. Errors are a dedicated PathError class (extends TypeError) that embeds the offending index and a link to a error-reference page, rather than throwing bare strings.
API Design — The five-function surface (parse, pathToRegexp, match, compile, stringify) is small and composable: each function takes and/or returns the same TokenData/pattern types, so you can parse once and feed the result into both match() and compile() without re-parsing. The tradeoff for that safety is a stricter, more explicit syntax than pre-v6 versions — ?, +, and bare regex characters are now reserved and throw PathError with a specific index and link, which front-loads migration pain but removes an entire class of ambiguous-pattern bugs.
Used by 20 apps in this directory
AFFiNE
Productivity · Project Management · Note Taking
Write, draw, and plan in one infinite canvas — the open-source alternative to Notion and Miro that keeps your data yours.
Appsmith
Developer Tools · Automation · No Code Platforms
Open-source low-code platform to build admin panels, dashboards, and internal tools connected to any database or API.
Baserow
No Code Platforms · Databases
Open-source no-code platform to build databases, apps, automations, and AI agents — self-hosted or cloud, with full data ownership.
Fern
Developer Tools
Fern turns a single OpenAPI, AsyncAPI, or Protobuf definition into type-safe SDKs for nine languages and a hosted API documentation site, all from one CLI and one source of truth.
Harness Open Source
Developer Tools · Devops · Code Editors
A unified open source DevOps platform combining Git hosting, CI/CD pipelines, cloud development environments, and artifact registries in a single self-hosted system.
marimo
Developer Tools · Data Engineering
A reactive Python notebook that eliminates hidden state, runs reproducibly, and deploys as a web app or script — stored as pure Python, built for the AI era.
Mastra Code
AI Code Assistants
"A coding agent that never compacts" — a terminal-based AI coding agent built on the Mastra framework, with Observational Memory instead of context compaction, multi-model support, and OAuth login for Claude Max or ChatGPT Plus.
medusa
Ecommerce
The most flexible open-source commerce platform — build B2C, B2B, and marketplace applications with modular, composable commerce primitives.
NocoBase
No Code Platforms · Low Code Platforms
Open-source AI + no-code platform that lets coding agents and people collaborate to build business systems fast on proven infrastructure.