Path-to-RegExp
Turn a path string such as /user/:name
into a regular expression.
![License](http://img.shields.io/npm/l/path-to-regexp.svg?style=flat)
Installation
npm install path-to-regexp --save
Usage
const { pathToRegexp, match, parse, compile } = require("path-to-regexp");
Path to regexp
The pathToRegexp
function returns a regular expression with keys
as a property. It accepts the following arguments:
- path A string.
- options (optional)
- sensitive Regexp will be case sensitive. (default:
false
) - trailing Allows optional trailing delimiter to match. (default:
true
) - strict Verify patterns are valid and safe to use. (default:
false
, recommended: true
) - end Match to the end of the string. (default:
true
) - start Match from the beginning of the string. (default:
true
) - loose Allow the delimiter to be arbitrarily repeated, e.g.
/
or ///
. (default: true
) - delimiter The default delimiter for segments, e.g.
[^/]
for :named
parameters. (default: '/'
) - encodePath A function for encoding input strings. (default:
x => x
, recommended: encodeurl
for unicode encoding)
const regexp = pathToRegexp("/foo/:bar");
Please note: The RegExp
returned by path-to-regexp
is intended for ordered data (e.g. pathnames, hostnames). It can not handle arbitrarily ordered data (e.g. query strings, URL fragments, JSON, etc).
Parameters
The path argument is used to define parameters and populate keys.
Named parameters
Named parameters are defined by prefixing a colon to the parameter name (:foo
). Parameter names can use any valid unicode identifier characters (similar to JavaScript).
const regexp = pathToRegexp("/:foo/:bar");
regexp.exec("/test/route");
Custom matching parameters
Parameters can have a custom regexp, which overrides the default match ([^/]+
). For example, you can match digits or names in a path:
const regexpNumbers = pathToRegexp("/icon-:foo(\\d+).png");
regexpNumbers.exec("/icon-123.png");
regexpNumbers.exec("/icon-abc.png");
const regexpWord = pathToRegexp("/(user|u)");
regexpWord.exec("/u");
regexpWord.exec("/users");
Tip: Backslashes need to be escaped with another backslash in JavaScript strings.
Unnamed parameters
It is possible to define a parameter without a name. The name will be numerically indexed:
const regexp = pathToRegexp("/:foo/(.*)");
regexp.exec("/test/route");
Custom prefix and suffix
Parameters can be wrapped in {}
to create custom prefixes or suffixes for your segment:
const regexp = pathToRegexp("{/:attr1}?{-:attr2}?{-:attr3}?");
regexp.exec("/test");
regexp.exec("/test-test");
Modifiers
Modifiers are used after parameters with custom prefixes and suffixes ({}
).
Optional
Parameters can be suffixed with a question mark (?
) to make the parameter optional.
const regexp = pathToRegexp("/:foo{/:bar}?");
regexp.exec("/test");
regexp.exec("/test/route");
Zero or more
Parameters can be suffixed with an asterisk (*
) to denote a zero or more parameter matches.
const regexp = pathToRegexp("{/:foo}*");
regexp.exec("/foo");
regexp.exec("/bar/baz");
One or more
Parameters can be suffixed with a plus sign (+
) to denote a one or more parameter matches.
const regexp = pathToRegexp("{/:foo}+");
regexp.exec("/");
regexp.exec("/bar/baz");
Custom separator
By default, parameters set the separator as the prefix + suffix
of the token. Using ;
you can modify this:
const regexp = pathToRegexp("/name{/:parts;-}+");
regexp.exec("/name");
regexp.exec("/bar/1-2-3");
Wildcard
A wildcard can also be used. It is roughly equivalent to (.*)
.
const regexp = pathToRegexp("/*");
regexp.exec("/");
regexp.exec("/bar/baz");
Match
The match
function returns a function for transforming paths into parameters:
- path A string.
- options (optional) The same options as
pathToRegexp
, plus:
- decode Function for decoding strings for params, or
false
to disable entirely. (default: decodeURIComponent
)
const fn = match("/user/:id");
fn("/user/123");
fn("/invalid");
fn("/user/caf%C3%A9");
Note: Setting decode: false
disables the "splitting" behavior of repeated parameters, which is useful if you need the exactly matched parameter back.
Compile ("Reverse" Path-To-RegExp)
The compile
function will return a function for transforming parameters into a valid path:
- path A string.
- options (optional) Similar to
pathToRegexp
(delimiter
, encodePath
, sensitive
, and loose
), plus:
- validate When
false
the function can produce an invalid (unmatched) path. (default: true
) - encode Function for encoding input strings for output into the path, or
false
to disable entirely. (default: encodeURIComponent
)
const toPath = compile("/user/:id");
toPath({ id: "name" });
toPath({ id: "café" });
const toPathRaw = compile("/user/:id", { encode: false });
toPathRaw({ id: "%3A%2F" });
toPathRaw({ id: ":/" });
const toPathRepeated = compile("{/:segment}+");
toPathRepeated({ segment: ["foo"] });
toPathRepeated({ segment: ["a", "b", "c"] });
const toPathRegexp = compile("/user/:id(\\d+)");
toPathRegexp({ id: "123" });
Developers
- If you are rewriting paths with match and compiler, consider using
encode: false
and decode: false
to keep raw paths passed around. - To ensure matches work on paths containing characters usually encoded, consider using encodeurl for
encodePath
. - If matches are intended to be exact, you need to set
loose: false
, trailing: false
, and sensitive: true
. - Enable
strict: true
to detect ReDOS issues.
Parse
A parse
function is available and returns TokenData
, the set of tokens and other metadata parsed from the input string. TokenData
is can passed directly into pathToRegexp
, match
, and compile
. It accepts only two options, delimiter
and encodePath
, which makes those options redundant in the above methods.
Tokens
The tokens
returned by TokenData
is an array of strings or keys, represented as objects, with the following properties:
name
The name of the tokenprefix
(optional) The prefix string for the segment (e.g. "/"
)suffix
(optional) The suffix string for the segment (e.g. ""
)pattern
(optional) The pattern defined to match this tokenmodifier
(optional) The modifier character used for the segment (e.g. ?
)separator
(optional) The string used to separate repeated parameters
Custom path
In some applications, you may not be able to use the path-to-regexp
syntax (e.g. file-based routing), but you can still use this library for match
, compile
, and pathToRegexp
by building your own TokenData
instance. For example:
import { TokenData, match } from "path-to-regexp";
const tokens = ["/", { name: "foo" }];
const path = new TokenData(tokens, "/");
const fn = match(path);
fn("/test");
Errors
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.
Unexpected ?
, *
, or +
In previous major versions /
and .
were used as implicit prefixes of parameters. So /:key?
was implicitly {/:key}?
. For example:
/:key?
→ {/:key}?
or /:key*
→ {/:key}*
or /:key+
→ {/:key}+
.:key?
→ {.:key}?
or .:key*
→ {.:key}*
or .:key+
→ {.:key}+
:key?
→ {:key}?
or :key*
→ {:key}*
or :key+
→ {:key}+
Unexpected ;
Used as a custom separator for repeated parameters.
Unexpected !
, @
, or ,
These characters have been reserved for future use.
Express <= 4.x
Path-To-RegExp breaks compatibility with Express <= 4.x
in the following ways:
- The only part of the string that is a regex is within
()
.
- In Express.js 4.x, everything was passed as-is after a simple replacement, so you could write
/[a-z]+
to match /test
.
- The
?
optional character must be used after {}
. - Some characters have new meaning or have been reserved (
{}?*+@!;
). - The parameter name now supports all unicode identifier characters, previously it was only
[a-z0-9]
.
License
MIT