# path-to-regexp — card for AI agents

Turns Express-style route paths like /user/:id into regular expressions and back.

Category: Utilities · npm package `path-to-regexp` · latest version 9.0.0 (checked 2026-11-01)
Install: `npm install path-to-regexp`
License: MIT (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Numbers (AgentGid data)
- Gid Score: 63.4 (#774 of all tools, 87 of 408 in its category)
- Used in 9 public projects set up for AI coding agents (0.1%)
- Downloads last week: 335,913,491 (as of 2026-11-24), +40.2% over 4 weeks
- GitHub stars: 8,620 (as of 2026-11-24)
- Last push: 2026-11-21

## Often used with
Husky 68.8%, lint-staged 68.8%, GraphQL.js 62.5%, Vite 68.8%, Testing Library 56.2%, Day.js 56.2%

## Links
- AgentGid page: https://agentgid.com/tools/path-to-regexp/
- Homepage: https://github.com/pillarjs/path-to-regexp#readme
- Repository: https://github.com/pillarjs/path-to-regexp


## path-to-regexp: match web addresses like /users/:id

path-to-regexp is a small free JavaScript library that checks a web address against route patterns such as /users/:id and reads out the parts, here id. It also works the other way and builds a correct address from those parts. Express uses this pattern style for its routes.

### Where to use it
- A small router without a framework: Your edge function or tiny server gets an address like /users/42 and needs to know which handler runs and which user was asked for.
- Build links from a route pattern: Menus and emails link to /blog/2026/hello-world; you fill the pattern once instead of gluing strings by hand, and odd characters get encoded.
- Pages with an optional part: One route answers both /image and /image.png, or both /users/delete and /users/7/delete.
- Lock whole sections behind a login: A check before every request decides if the address is under /admin or /account and sends visitors without a session to the sign-in page. (idea, not from the official pages)
- Move route paths to Express 5: When an older Express app moves to version 5, routes such as /* or /:ext? must be rewritten in the new pattern style.

### How to start
- Add the package to your project.

```
npm install path-to-regexp --save
```

- Make a matcher from a pattern and call it with an address; you get the parts as params, or false.

```
import { match, pathToRegexp, compile, parse, stringify } from "path-to-regexp";

const fn = match("/:foo/:bar");

fn("/test/route");
//=> { path: '/test/route', params: { foo: 'test', bar: 'route' } }
```


### License
Free to use, change and sell as part of your product under the MIT license. Keep its copyright and license notice in copies of the package you ship.

### Good to know
- Version 9 runs only as an ES module and needs Node.js 22 or newer.
- It is for the path part of an address only. The part after a question mark, such as ?tab=posts, has to be read separately.
- The pattern style changed in version 8. Old patterns with ?, an unnamed * or brackets now stop with an error.
- Older versions had several slow-pattern security issues; 0.1.13 and 8.4.0 contain the fixes.
- By default, matching ignores upper and lower case and accepts a slash at the end.

Checked 2026-11-11 against: https://github.com/pillarjs/path-to-regexp/blob/master/Readme.md, https://github.com/pillarjs/path-to-regexp/blob/master/LICENSE, https://github.com/pillarjs/path-to-regexp/releases/tag/v9.0.0, https://github.com/pillarjs/path-to-regexp/releases/tag/v8.0.0, https://github.com/pillarjs/path-to-regexp/security/advisories, https://github.com/pillarjs/path-to-regexp/security/advisories/GHSA-37ch-88jc-xwx2, https://github.com/pillarjs/path-to-regexp/security/advisories/GHSA-j3q9-mxjg-w52f, https://github.com/pillarjs/path-to-regexp/security/advisories/GHSA-27v5-c462-wpq7, https://expressjs.com/en/guide/migrating-5/, https://registry.npmjs.org/path-to-regexp/latest

### Mistakes to avoid
- Don't: match('/users/:id?') or app.get('/*', ...) written in the Express 4 style → Do: Use braces for optional parts and name every wildcard, e.g. '/users{/:id}' and '/*splat' (or '/{*splat}' to include '/') (Since version 8 these characters are reserved; in our run 9.0.0 threw 'TypeError Unexpected ? at index 10' and 'Missing parameter name'.)
- Don't: const { match } = require('path-to-regexp') on version 9, or running it on Node.js 20 → Do: Use import on Node.js 22+, or pin path-to-regexp@8.4.2 for a CommonJS project on older Node.js (Version 9.0.0 ships only an ES module build and requires Node.js 22 or newer.)
- Don't: match('/users/:id')(request.url) with the query string still attached → Do: Match only the pathname, e.g. new URL(request.url).pathname, and read the query with URLSearchParams (In our run '/users/42?tab=posts' gave id '42?tab=posts'; the README says query strings are not handled.)
- Don't: Listing '/users/:id' before '/users{/:id}/delete' and expecting /users/delete to reach the delete handler → Do: Put the more specific routes first, or check them in a fixed order you control (A parameter takes any text up to the next slash, so in our run /users/delete matched '/users/:id' with id 'delete'.)
- Don't: Staying on path-to-regexp 0.1.12 or 8.0–8.3, or building route patterns from user input → Do: Use 0.1.13 (Express 4) or 8.4.0+ / 9.x, and only use patterns written by you (Several advisories describe patterns that make the generated check very slow, a denial-of-service risk.)

### Questions
- What is the difference between /users/:id and /docs/*path? :id matches one part between slashes; *path matches one or more parts and gives them back as a list.
- How do I make part of a route optional? Wrap it in braces, for example /users{/:id}/delete or /:file{.:ext}. The old ? suffix is not supported.
- Which version does Express use? Express 5 moved to the new pattern style; Express 4 stays on the old 0.1 line, which got its latest fix in 0.1.13.
- What does it do with two parts in one segment, like /:name.:ext? Since version 9 the last part takes the extra text, so /foo.json.html gives name foo and ext json.html.
- Does it decode characters like %C3%A9? Yes. match() decodes parameters with decodeURIComponent and compile() encodes them, unless you turn that off.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
