{"id":13396709,"url":"https://github.com/pillarjs/path-to-regexp","last_synced_at":"2026-04-02T19:11:21.536Z","repository":{"id":4151779,"uuid":"5265803","full_name":"pillarjs/path-to-regexp","owner":"pillarjs","description":"Turn a path string such as `/user/:name` into a regular expression","archived":false,"fork":false,"pushed_at":"2025-04-29T22:21:58.000Z","size":1023,"stargazers_count":8375,"open_issues_count":7,"forks_count":417,"subscribers_count":67,"default_branch":"master","last_synced_at":"2025-05-04T11:52:57.313Z","etag":null,"topics":["expressjs","nodejs","path-to-regexp","router"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pillarjs.png","metadata":{"files":{"readme":"Readme.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"open_collective":"express"}},"created_at":"2012-08-01T22:47:17.000Z","updated_at":"2025-05-04T05:57:50.000Z","dependencies_parsed_at":"2022-07-08T04:47:53.607Z","dependency_job_id":"26ad3b5b-0459-4d8e-8c1b-a6d2d22b8c95","html_url":"https://github.com/pillarjs/path-to-regexp","commit_stats":{"total_commits":316,"total_committers":35,"mean_commits":9.028571428571428,"dds":0.1392405063291139,"last_synced_commit":"d63f44bc54f2c6fe7d35dc77f8515622006bf7cb"},"previous_names":[],"tags_count":67,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pillarjs%2Fpath-to-regexp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pillarjs%2Fpath-to-regexp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pillarjs%2Fpath-to-regexp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pillarjs%2Fpath-to-regexp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pillarjs","download_url":"https://codeload.github.com/pillarjs/path-to-regexp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252330682,"owners_count":21730688,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["expressjs","nodejs","path-to-regexp","router"],"created_at":"2024-07-30T18:01:00.711Z","updated_at":"2026-04-02T19:11:21.481Z","avatar_url":"https://github.com/pillarjs.png","language":"TypeScript","funding_links":["https://opencollective.com/express"],"categories":["路径","第三方库介绍","TypeScript","Repository","js库(Browser/Nodejs)","工具类","工具类库","Utilities","目录","Route"],"sub_categories":["工具库","RegExp/Glob","macros","React Components"],"readme":"# Path-to-RegExp\n\n\u003e Turn a path string such as `/user/:name` into a regular expression.\n\n[![NPM version][npm-image]][npm-url]\n[![NPM downloads][downloads-image]][downloads-url]\n[![Build status][build-image]][build-url]\n[![Build coverage][coverage-image]][coverage-url]\n[![License][license-image]][license-url]\n\n## Installation\n\n```\nnpm install path-to-regexp --save\n```\n\n## Usage\n\n```js\nconst {\n  match,\n  pathToRegexp,\n  compile,\n  parse,\n  stringify,\n} = require(\"path-to-regexp\");\n```\n\n### Parameters\n\nParameters match arbitrary strings in a path by matching up to the end of the segment, or up to any proceeding tokens. They are defined by prefixing a colon to the parameter name (`:foo`). Parameter names can use any valid JavaScript identifier, or be double quoted to use other characters (`:\"param-name\"`).\n\n```js\nconst fn = match(\"/:foo/:bar\");\n\nfn(\"/test/route\");\n//=\u003e { path: '/test/route', params: { foo: 'test', bar: 'route' } }\n```\n\n### Wildcard\n\nWildcard parameters match one or more characters across multiple segments. They are defined the same way as regular parameters, but are prefixed with an asterisk (`*foo`).\n\n```js\nconst fn = match(\"/*splat\");\n\nfn(\"/bar/baz\");\n//=\u003e { path: '/bar/baz', params: { splat: [ 'bar', 'baz' ] } }\n```\n\n### Optional\n\nBraces can be used to define parts of the path that are optional.\n\n```js\nconst fn = match(\"/users{/:id}/delete\");\n\nfn(\"/users/delete\");\n//=\u003e { path: '/users/delete', params: {} }\n\nfn(\"/users/123/delete\");\n//=\u003e { path: '/users/123/delete', params: { id: '123' } }\n```\n\n## Match\n\nThe `match` function returns a function for matching strings against a path:\n\n- **path** String or array of strings.\n- **options** _(optional)_ (Extends [pathToRegexp](#pathToRegexp) options)\n  - **decode** Function for decoding strings to params, or `false` to disable all processing. (default: `decodeURIComponent`)\n\n```js\nconst fn = match(\"/foo/:bar\");\n```\n\n**Please note:** `path-to-regexp` is intended for ordered data (e.g. paths, hosts). It can not handle arbitrarily ordered data (e.g. query strings, URL fragments, JSON, etc).\n\n## PathToRegexp\n\nThe `pathToRegexp` function returns the `regexp` for matching strings against paths, and an array of `keys` for understanding the `RegExp#exec` matches.\n\n- **path** String or array of strings.\n- **options** _(optional)_ (See [parse](#parse) for more options)\n  - **sensitive** Regexp will be case sensitive. (default: `false`)\n  - **end** Validate the match reaches the end of the string. (default: `true`)\n  - **delimiter** The default delimiter for segments, e.g. `[^/]` for `:named` parameters. (default: `'/'`)\n  - **trailing** Allows optional trailing delimiter to match. (default: `true`)\n\n```js\nconst { regexp, keys } = pathToRegexp(\"/foo/:bar\");\n\nregexp.exec(\"/foo/123\"); //=\u003e [\"/foo/123\", \"123\"]\n```\n\n## Compile (\"Reverse\" Path-To-RegExp)\n\nThe `compile` function will return a function for transforming parameters into a valid path:\n\n- **path** A string.\n- **options** (See [parse](#parse) for more options)\n  - **delimiter** The default delimiter for segments, e.g. `[^/]` for `:named` parameters. (default: `'/'`)\n  - **encode** Function for encoding input strings for output into the path, or `false` to disable entirely. (default: `encodeURIComponent`)\n\n```js\nconst toPath = compile(\"/user/:id\");\n\ntoPath({ id: \"name\" }); //=\u003e \"/user/name\"\ntoPath({ id: \"café\" }); //=\u003e \"/user/caf%C3%A9\"\n\nconst toPathRepeated = compile(\"/*segment\");\n\ntoPathRepeated({ segment: [\"foo\"] }); //=\u003e \"/foo\"\ntoPathRepeated({ segment: [\"a\", \"b\", \"c\"] }); //=\u003e \"/a/b/c\"\n\n// When disabling `encode`, you need to make sure inputs are encoded correctly. No arrays are accepted.\nconst toPathRaw = compile(\"/user/:id\", { encode: false });\n\ntoPathRaw({ id: \"%3A%2F\" }); //=\u003e \"/user/%3A%2F\"\n```\n\n## Stringify\n\nTransform `TokenData` (a sequence of tokens) back into a Path-to-RegExp string.\n\n- **data** A `TokenData` instance\n\n```js\nconst data = new TokenData([\n  { type: \"text\", value: \"/\" },\n  { type: \"param\", name: \"foo\" },\n]);\n\nconst path = stringify(data); //=\u003e \"/:foo\"\n```\n\n## Developers\n\n- If you are rewriting paths with match and compile, consider using `encode: false` and `decode: false` to keep raw paths passed around.\n- To ensure matches work on paths containing characters usually encoded, such as emoji, consider using [encodeurl](https://github.com/pillarjs/encodeurl) for `encodePath`.\n\n### Parse\n\nThe `parse` function accepts a string and returns `TokenData`, which can be used with `match` and `compile`.\n\n- **path** A string.\n- **options** _(optional)_\n  - **encodePath** A function for encoding input strings. (default: `x =\u003e x`, recommended: [`encodeurl`](https://github.com/pillarjs/encodeurl))\n\n### Tokens\n\n`TokenData` is a sequence of tokens, currently of types `text`, `parameter`, `wildcard`, or `group`.\n\n### Custom path\n\nIn some applications, you may not be able to use the `path-to-regexp` syntax, but still want to use this library for `match` and `compile`. For example:\n\n```js\nimport { TokenData, match } from \"path-to-regexp\";\n\nconst tokens = [\n  { type: \"text\", value: \"/\" },\n  { type: \"parameter\", name: \"foo\" },\n];\nconst path = new TokenData(tokens);\nconst fn = match(path);\n\nfn(\"/test\"); //=\u003e { path: '/test', index: 0, params: { foo: 'test' } }\n```\n\n## Errors\n\nAn effort has been made to ensure ambiguous paths from previous releases throw an error. This means you might be seeing an error when things worked before.\n\n### Unexpected `?` or `+`\n\nIn past releases, `?`, `*`, and `+` were used to denote optional or repeating parameters. As an alternative, try these:\n\n- For optional (`?`), use an empty segment in a group such as `/:file{.:ext}`.\n- For repeating (`+`), only wildcard matching is supported, such as `/*path`.\n- For optional repeating (`*`), use a group and a wildcard parameter such as `/files{/*path}`.\n\n### Unexpected `(`, `)`, `[`, `]`, etc.\n\nPrevious versions of Path-to-RegExp used these for RegExp features. This version no longer supports them so they've been reserved to avoid ambiguity. To use these characters literally, escape them with a backslash, e.g. `\"\\\\(\"`.\n\n### Missing parameter name\n\nParameter names must be provided after `:` or `*`, and they must be a valid JavaScript identifier. If you want an parameter name that isn't a JavaScript identifier, such as starting with a number, you can wrap the name in quotes like `:\"my-name\"`.\n\n### Unterminated quote\n\nParameter names can be wrapped in double quote characters, and this error means you forgot to close the quote character.\n\n### Express \u003c= 4.x\n\nPath-To-RegExp breaks compatibility with Express \u003c= `4.x` in the following ways:\n\n- The wildcard `*` must have a name, matching the behavior of parameters `:`.\n- The optional character `?` is no longer supported, use braces instead: `/:file{.:ext}`.\n- Regexp characters are not supported.\n- Some characters have been reserved to avoid confusion during upgrade (`()[]?+!`).\n- Parameter names now support valid JavaScript identifiers, or quoted like `:\"this\"`.\n\n## License\n\nMIT\n\n[npm-image]: https://img.shields.io/npm/v/path-to-regexp\n[npm-url]: https://npmjs.org/package/path-to-regexp\n[downloads-image]: https://img.shields.io/npm/dm/path-to-regexp\n[downloads-url]: https://npmjs.org/package/path-to-regexp\n[build-image]: https://img.shields.io/github/actions/workflow/status/pillarjs/path-to-regexp/ci.yml?branch=master\n[build-url]: https://github.com/pillarjs/path-to-regexp/actions/workflows/ci.yml?query=branch%3Amaster\n[coverage-image]: https://img.shields.io/codecov/c/gh/pillarjs/path-to-regexp\n[coverage-url]: https://codecov.io/gh/pillarjs/path-to-regexp\n[license-image]: http://img.shields.io/npm/l/path-to-regexp.svg?style=flat\n[license-url]: LICENSE.md\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpillarjs%2Fpath-to-regexp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpillarjs%2Fpath-to-regexp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpillarjs%2Fpath-to-regexp/lists"}