📄 Readme.md

← 返回目录

Path-to-RegExp

Turn a path string such as /user/:name into a regular expression.

[![NPM version][npm-image]][npm-url] [![NPM downloads][downloads-image]][downloads-url] [![Build status][build-image]][build-url] [![Build coverage][coverage-image]][coverage-url] [![License][license-image]][license-url]

Installation

npm install path-to-regexp --save

Usage

const {
  match,
  pathToRegexp,
  compile,
  parse,
  stringify,
} = require("path-to-regexp");

Parameters

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").

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

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

Wildcard

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).

const fn = match("/*splat");

fn("/bar/baz"); //=> { path: '/bar/baz', params: { splat: [ 'bar', 'baz' ] } }

Optional

Braces can be used to define parts of the path that are optional.

const fn = match("/users{/:id}/delete");

fn("/users/delete"); //=> { path: '/users/delete', params: {} }

fn("/users/123/delete"); //=> { path: '/users/123/delete', params: { id: '123' } }

Match

The match function returns a function for matching strings against a path:

- path String, TokenData object, or array of strings and TokenData objects.

- decode Function for decoding strings to params, or false to disable all processing. (default: decodeURIComponent)

const 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).

PathToRegexp

The pathToRegexp function returns the regexp for matching strings against paths, and an array of keys for understanding the RegExp#exec matches.

- path String, TokenData object, or array of strings and TokenData objects.

- sensitive Regexp will be case sensitive. (default: false) - end Validate the match reaches the end of the string. (default: true) - delimiter The default delimiter for segments, e.g. [^/] for :named parameters. (default: '/') - trailing Allows optional trailing delimiter to match. (default: true)

const { regexp, keys } = pathToRegexp("/foo/:bar");

regexp.exec("/foo/123"); //=> ["/foo/123", "123"]

Compile ("Reverse" Path-To-RegExp)

The compile function will return a function for transforming parameters into a valid path:

- path A string or TokenData object.

- delimiter The default delimiter for segments, e.g. [^/] for :named parameters. (default: '/') - 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" }); //=> "/user/name" toPath({ id: "café" }); //=> "/user/caf%C3%A9"

const toPathRepeated = compile("/*segment");

toPathRepeated({ segment: ["foo"] }); //=> "/foo" toPathRepeated({ segment: ["a", "b", "c"] }); //=> "/a/b/c"

// When disabling encode, you need to make sure inputs are encoded correctly. No arrays are accepted. const toPathRaw = compile("/user/:id", { encode: false });

toPathRaw({ id: "%3A%2F" }); //=> "/user/%3A%2F"

Stringify

Transform a TokenData object to a Path-to-RegExp string.

- data A TokenData object.

const data = {
  tokens: [
    { type: "text", value: "/" },
    { type: "param", name: "foo" },
  ],
};

const path = stringify(data); //=> "/:foo"

Developers

- If you are rewriting paths with match and compile, consider using encode: false and decode: false to keep raw paths passed around.

- encodePath A function for encoding input strings. (default: x => x, recommended: encodeurl)

Tokens

TokenData has two properties:

- tokens A sequence of tokens, currently of types text, parameter, wildcard, or group.

{ type: "text", value: "/" }, { type: "parameter", name: "foo" }, ]; const originalPath = "/[foo]"; // To help debug error messages. const path = { tokens, originalPath }; const fn = match(path);

fn("/test"); //=> { path: '/test', index: 0, params: { foo: '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.

Missing parameter name

Parameter names must be provided after : or , for example /path. They can be valid JavaScript identifiers (e.g. :myName) or JSON strings (:"my-name").

Unexpected ? or +

In past releases, ?, *, and + were used to denote optional or repeating parameters. As an alternative, try these:

- For optional (?), use braces: /file{.:ext}.

[npm-url]: https://npmjs.org/package/path-to-regexp [downloads-image]: https://img.shields.io/npm/dm/path-to-regexp [downloads-url]: https://npmjs.org/package/path-to-regexp [build-image]: https://img.shields.io/github/actions/workflow/status/pillarjs/path-to-regexp/ci.yml?branch=master [build-url]: https://github.com/pillarjs/path-to-regexp/actions/workflows/ci.yml?query=branch%3Amaster [coverage-image]: https://img.shields.io/codecov/c/gh/pillarjs/path-to-regexp [coverage-url]: https://codecov.io/gh/pillarjs/path-to-regexp [license-image]: http://img.shields.io/npm/l/path-to-regexp.svg?style=flat [license-url]: LICENSE.md