Gathering detailed insights and metrics for path-to-regexp
Gathering detailed insights and metrics for path-to-regexp
Gathering detailed insights and metrics for path-to-regexp
Gathering detailed insights and metrics for path-to-regexp
@qixian.cs/path-to-regexp
Express style path to RegExp utility
path-match
wrapper around path-to-regexp for easy route parameters
micromatch
Glob matching for javascript/node.js. A replacement and faster alternative to minimatch and multimatch.
@stdlib/regexp-extended-length-path
Regular expression to detect an extended-length path.
Turn a path string such as `/user/:name` into a regular expression
npm install path-to-regexp
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.8
Supply Chain
99.5
Quality
90.9
Maintenance
100
Vulnerability
100
License
Fix backtracking (again)
Published on 05 Dec 2024
8.2.0
Published on 26 Sept 2024
Error on bad input
Published on 12 Sept 2024
Fix backtracking in 6.x
Published on 12 Sept 2024
Add backtracking protection
Published on 10 Sept 2024
Fix backtracking in 1.x
Published on 10 Sept 2024
TypeScript (100%)
Total Downloads
10,768,234,343
Last Day
11,449,076
Last Week
51,953,469
Last Month
228,966,815
Last Year
2,706,882,253
8,268 Stars
343 Commits
389 Forks
64 Watching
6 Branches
51 Contributors
Minified
Minified + Gzipped
Latest Version
8.2.0
Package Id
path-to-regexp@8.2.0
Unpacked Size
53.95 kB
Size
13.17 kB
File Count
6
NPM Version
10.5.0
Node Version
20.12.1
Publised On
26 Sept 2024
Cumulative downloads
Total Downloads
Last day
-1.3%
11,449,076
Compared to previous day
Last week
-13.5%
51,953,469
Compared to previous week
Last month
4.7%
228,966,815
Compared to previous month
Last year
13.7%
2,706,882,253
Compared to previous year
Turn a path string such as
/user/:name
into a regular expression.
npm install path-to-regexp --save
1const { 2 match, 3 pathToRegexp, 4 compile, 5 parse, 6 stringify, 7} = require("path-to-regexp");
Parameters 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"
).
1const fn = match("/:foo/:bar"); 2 3fn("/test/route"); 4//=> { path: '/test/route', params: { foo: 'test', bar: 'route' } }
Wildcard 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
).
1const fn = match("/*splat"); 2 3fn("/bar/baz"); 4//=> { path: '/bar/baz', params: { splat: [ 'bar', 'baz' ] } }
Braces can be used to define parts of the path that are optional.
1const fn = match("/users{/:id}/delete"); 2 3fn("/users/delete"); 4//=> { path: '/users/delete', params: {} } 5 6fn("/users/123/delete"); 7//=> { path: '/users/123/delete', params: { id: '123' } }
The match
function returns a function for matching strings against a path:
false
to disable all processing. (default: decodeURIComponent
)1const fn = match("/foo/:bar");
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).
The pathToRegexp
function returns a regular expression for matching strings against paths. It
false
)true
)[^/]
for :named
parameters. (default: '/'
)true
)1const { regexp, keys } = pathToRegexp("/foo/:bar");
The compile
function will return a function for transforming parameters into a valid path:
[^/]
for :named
parameters. (default: '/'
)false
to disable entirely. (default: encodeURIComponent
)1const toPath = compile("/user/:id"); 2 3toPath({ id: "name" }); //=> "/user/name" 4toPath({ id: "café" }); //=> "/user/caf%C3%A9" 5 6const toPathRepeated = compile("/*segment"); 7 8toPathRepeated({ segment: ["foo"] }); //=> "/foo" 9toPathRepeated({ segment: ["a", "b", "c"] }); //=> "/a/b/c" 10 11// When disabling `encode`, you need to make sure inputs are encoded correctly. No arrays are accepted. 12const toPathRaw = compile("/user/:id", { encode: false }); 13 14toPathRaw({ id: "%3A%2F" }); //=> "/user/%3A%2F"
Transform TokenData
(a sequence of tokens) back into a Path-to-RegExp string.
TokenData
instance1const data = new TokenData([ 2 { type: "text", value: "/" }, 3 { type: "param", name: "foo" }, 4]); 5 6const path = stringify(data); //=> "/:foo"
encode: false
and decode: false
to keep raw paths passed around.encodePath
.The parse
function accepts a string and returns TokenData
, the set of tokens and other metadata parsed from the input string. TokenData
is can used with match
and compile
.
x => x
, recommended: encodeurl
)TokenData
is a sequence of tokens, currently of types text
, parameter
, wildcard
, or group
.
In 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:
1import { TokenData, match } from "path-to-regexp"; 2 3const tokens = [ 4 { type: "text", value: "/" }, 5 { type: "parameter", name: "foo" }, 6]; 7const path = new TokenData(tokens); 8const fn = match(path); 9 10fn("/test"); //=> { path: '/test', index: 0, params: { foo: 'test' } }
An 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.
?
or +
In past releases, ?
, *
, and +
were used to denote optional or repeating parameters. As an alternative, try these:
?
), use an empty segment in a group such as /:file{.:ext}
.+
), only wildcard matching is supported, such as /*path
.*
), use a group and a wildcard parameter such as /files{/*path}
.(
, )
, [
, ]
, etc.Previous 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. "\\("
.
Parameter 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"
.
Parameter names can be wrapped in double quote characters, and this error means you forgot to close the quote character.
Path-To-RegExp breaks compatibility with Express <= 4.x
in the following ways:
*
must have a name, matching the behavior of parameters :
.?
is no longer supported, use braces instead: /:file{.:ext}
.()[]?+!
).:"this"
.MIT
Stable Version
6
0/10
Summary
Unpatched `path-to-regexp` ReDoS in 0.1.x
Affected Versions
< 0.1.12
Patched Versions
0.1.12
7.5/10
Summary
path-to-regexp outputs backtracking regular expressions
Affected Versions
>= 4.0.0, < 6.3.0
Patched Versions
6.3.0
7.5/10
Summary
path-to-regexp outputs backtracking regular expressions
Affected Versions
>= 7.0.0, < 8.0.0
Patched Versions
8.0.0
7.5/10
Summary
path-to-regexp outputs backtracking regular expressions
Affected Versions
>= 2.0.0, < 3.3.0
Patched Versions
3.3.0
7.5/10
Summary
path-to-regexp outputs backtracking regular expressions
Affected Versions
>= 0.2.0, < 1.9.0
Patched Versions
1.9.0
7.5/10
Summary
path-to-regexp outputs backtracking regular expressions
Affected Versions
< 0.1.10
Patched Versions
0.1.10
Reason
no binaries found in the repo
Reason
2 out of 2 merged PRs checked by a CI test -- score normalized to 10
Reason
27 different organizations found -- score normalized to 10
Details
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
GitHub workflow tokens follow principle of least privilege
Details
Reason
3 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 6
Details
Reason
1 commit(s) out of 30 and 2 issue activity out of 30 found in the last 90 days -- score normalized to 2
Reason
branch protection not enabled on development/release branches
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
found 28 unreviewed changesets out of 30 -- score normalized to 0
Reason
no update tool detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-01-27T21:24:10Z
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More