path-to-regexp

Turn path strings like /user/:id into regular expressions for matching, building, and parsing URLs.

Library
npm
v8.4.2
8,610 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
64 /100 Good
Development Activity 60
Maintenance 36
Community 60
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
75 /100 Good
Architecture 80
Code Quality 78
Innovation 68
Learning Curve 72

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 a keys array describing each capture group
  • match() — a higher-level matcher that returns a function taking a string and returning { path, params } or false
  • compile() — the reverse operation: turns a pattern into a function that builds a concrete path string from a params object
  • parse() / stringify() — tokenize a pattern into a TokenData structure 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 21 apps in this directory

TypeScript
82%
Other

AFFiNE

Collaboration · Note Taking · Productivity

73,040

Write, draw, and plan in one infinite canvas — the open-source alternative to Notion and Miro that keeps your data yours.

View details
91
Repo Health
87
Technical
67
Dependency
Built with
TypeScript 82%
Rust 11%
Updated 1 weeks ago
TypeScript
68%
Apache 2.0

Appsmith

Automation · Developer Tools · No Code Platforms

40,959

Open-source low-code platform to build admin panels, dashboards, and internal tools connected to any database or API.

View details
93
Repo Health
79
Technical
66
Dependency
Built with
TypeScript 68%
Java 21%
Updated 1 weeks ago
Python
68%
Other

Baserow

Databases · No Code Platforms

6,012

Open-source no-code platform to build databases, apps, automations, and AI agents — self-hosted or cloud, with full data ownership.

View details
89
Repo Health
84
Technical
68
Dependency
Built with
Python 68%
JavaScript 16%
Vue 11%
Updated 1 weeks ago
TypeScript
75%
Apache 2.0

Fern

Developer Tools

3,787

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.

View details
90
Repo Health
86
Technical
66
Dependency
Built with
TypeScript 75%
Updated 1 weeks ago
Go
62%
Apache 2.0

Harness Open Source

Code Editors · Developer Tools · Devops

38,450

A unified open source DevOps platform combining Git hosting, CI/CD pipelines, cloud development environments, and artifact registries in a single self-hosted system.

View details
89
Repo Health
79
Technical
64
Dependency
Built with
Go 62%
TypeScript 33%
Updated 2 weeks ago
TypeScript
44%
Other

Magic

AI Agents · Automation · Low Code Platforms

5,043

Magic is an enterprise-grade open-source AI agent platform combining a generalist AI agent, workflow engine, IM, and collaborative office system for running an AI-powered digital workforce.

View details
70
Repo Health
79
Technical
65
Dependency
Built with
TypeScript 44%
PHP 32%
Updated 1 months ago
Python
62%
Apache 2.0

marimo

Data Engineering · Developer Tools

22,918

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.

View details
89
Repo Health
91
Technical
65
Dependency
Built with
Python 62%
TypeScript 37%
Updated 1 weeks ago
TypeScript
99%
Apache 2.0

Mastra Code

AI Code Assistants

28,378

"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.

View details
90
Repo Health
73
Technical
65
Dependency
Built with
TypeScript 99%
Updated 1 weeks ago
TypeScript
88%
MIT

medusa

Ecommerce

36,494

The most flexible open-source commerce platform — build B2C, B2B, and marketplace applications with modular, composable commerce primitives.

View details
93
Repo Health
87
Technical
63
Dependency
Built with
TypeScript 88%
JavaScript 12%
Updated 2 weeks ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers