Install path-to-regexp via npm
masterInstall the path-to-regexp package using npm to turn path strings into regular expressions.
npm install path-to-regexp --saverepository·master·Indexed 27 days ago
https://github.com/pillarjs/path-to-regexpAn Express-style utility for converting path strings (e.g., /user/:id) into regular expressions for matching and parsing. Version 8.4.2 provides functions to match strings against paths, generate RegExps and keys via pathToRegexp(), transform parameters back into paths using compile(), and parse path strings into TokenData.
Install the path-to-regexp package using npm to turn path strings into regular expressions.
npm install path-to-regexp --saveParameters match arbitrary strings in a path up to the end of the segment or the next token. Define them by prefixing a colon to the name (:foo).
:foo:"param-name"Wildcard parameters match one or more characters across multiple segments. Prefix them with an asterisk (*foo).
{} to define parts of a path that are optional.Parameter names must follow : or *. Example: /*path is valid; /* is not.
? or + symbolsThese are no longer supported for optional or repeating parameters. Use braces instead:
?): Use /file{.:ext} instead of /file.:ext?.+): Use a wildcard /*path.*): Use /files{/*path}.Characters like (, ), [, and ] are no longer supported for RegExp features and are reserved. To match them literally, escape them with a backslash (e.g., "\\(")
If migrating from Express <= 4.x, note that:
* must have a name.? character is unsupported (use braces).()[]?+!.The compile function (often called "Reverse" Path-To-RegExp) returns a function for transforming parameter objects into valid path strings.
Parameters:
String or TokenData object.'/')false to disable entirely. (default: encodeURIComponent)const { compile } = require("path-to-regexp");
const toPath = compile("/user/:id");
toPath({ id: "name" }); //=> "/user/name"
toPath({ id: "café" }); //=> "/user/caf%C3%A9"
// Using wildcards with compile
const toPathRepeated = compile("/*segment");
toPathRepeated({ segment: ["a", "b", "c"] }); //=> "/a/b/c"
// Disabling encoding
const toPathRaw = compile("/user/:id", { encode: false });
toPathRaw({ id: "%3A%2F" }); //=> "/user/%3A%2F"The match function returns a function that takes a string and returns an object containing the matched path and its parameters.
Parameters:
String, TokenData object, or an array of strings and TokenData objects.false to disable all processing. (default: decodeURIComponent)The pathToRegexp function returns a RegExp for matching strings against paths, and an array of keys to help understand the RegExp#exec matches.
Parameters:
String, TokenData object, or an array of strings and TokenData objects.false)true)[^/] for :named parameters. (default: '/')true)const { pathToRegexp } = require("path-to-regexp");
const { regexp, keys } = pathToRegexp("/foo/:bar");
regexp.exec("/foo/123"); //=> ["/foo/123", "123"]The parse function accepts a string and returns a TokenData object, which can be used with match and compile functions.
Parameters:
String.x => x, recommended: encodeurl)The stringify function transforms a TokenData object back into a Path-to-RegExp string.
Parameters:
TokenData object.const { stringify } = require("path-to-regexp");
const data = {
tokens: [
{ type: "text", value: "/" },
{ type: "param", name: "foo" },
],
};
const path = stringify(data); //=> "/:foo"parse() function breaks down a path string into a sequence of Token objects (Text, Parameter, Wildcard, or Group). This provides a structured representation of the path's components.match() function transforms a path pattern into a function that tests an input string. If the string matches the pattern, it returns a MatchResult containing the matched path and the extracted parameters. If it does not match, it returns false.pathToRegexp() to transform a path string (or an array of path strings) into a regular expression and a list of associated keys. This is useful for low-level regex matching logic.