explaingit

pillarjs/path-to-regexp

8,587TypeScriptAudience · developerComplexity · 2/5LicenseSetup · easy

TLDR

A small JavaScript library that converts URL path patterns like /user/:name into regular expressions for matching URLs and extracting named parameters, used internally by Express.js and other Node routers.

Mindmap

mindmap
  root((repo))
    What it does
      URL pattern matching
      Named param extraction
      URL generation
    Key functions
      match
      compile
      parse
    Pattern features
      Named params
      Wildcard params
      Optional sections
    Used by
      Express.js
      Node routers
      npm ecosystem
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Match incoming request URLs against route patterns and extract named values like user IDs in a web server.

USE CASE 2

Generate valid URLs from a route pattern and parameter values using the compile function.

USE CASE 3

Parse a route pattern into tokens to inspect or transform its structure programmatically.

Tech stack

TypeScriptJavaScriptNode.js

Getting it running

Difficulty · easy Time to first run · 5min

npm install path-to-regexp is the only step, no external services or configuration required.

MIT license: use freely for any purpose, including commercial use, as long as you keep the copyright notice.

In plain English

Path-to-RegExp is a small JavaScript library that takes a URL path pattern, like /user/:name, and turns it into a regular expression that can test whether an actual URL matches that pattern, and if so, pull out the named parts. For example, if you define a route as /user/:name, this library can tell you that /user/alice matches and that the name value is alice. This kind of matching is the core mechanism behind how web frameworks know which function to run when someone visits a particular URL. The library provides a few related tools. The match function lets you check whether a real path fits your pattern and gives you back the captured values. The compile function does the reverse: you give it a pattern and some values, and it builds a valid URL from them, handling encoding of special characters automatically. There is also a parse function that breaks a pattern down into tokens for cases where you need to work with the structure more directly. Path patterns support several features described in the README. Named parameters (prefixed with a colon, like :id) capture the value at that position in the URL. Wildcard parameters (prefixed with an asterisk, like *splat) match across multiple path segments. Optional sections can be wrapped in braces, so /users{/:id}/delete matches both /users/delete and /users/123/delete. This library is closely connected to the Express.js web framework and other Node.js routers, which use it internally to handle routing. The current version breaks compatibility with Express 4.x and earlier in a few ways, and the README documents those differences along with error messages you might see if you are upgrading from an older version. The project is published on npm as path-to-regexp and is released under the MIT license.

Copy-paste prompts

Prompt 1
Show me how to use path-to-regexp match() to check if /user/alice matches the pattern /user/:name and pull out the name value.
Prompt 2
How do I use path-to-regexp compile() to build a URL string from the pattern /user/:id/posts/:postId with specific values?
Prompt 3
What wildcard and optional syntax does path-to-regexp support, and show me an example of matching /users/delete and /users/123/delete with one pattern.
Prompt 4
What are the breaking changes in path-to-regexp that affect Express 4.x routes, and how do I update my existing patterns?
Open on GitHub → Explain another repo

← pillarjs on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.