Skip to content

MM56/path-to-regexp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Path-to-RegExp

Turn an Express-style path string such as /user/:name into a regular expression.

Build Status NPM version

Usage

var pathToRegexp = require('path-to-regexp'); // pathToRegexp(path, keys, options);
  • path A string in the express format, an array of strings, or a regular expression.
  • keys An array to be populated with the keys present in the url.
  • options
    • options.sensitive When set to true the route will be case sensitive.
    • options.strict When set to true a trailing slash will affect the url matching.
    • options.end When set to false the url will match only the prefix.
var keys = []; var re = pathToRegexp('/foo/:bar', keys); // re = /^\/foo\/(?:([^\/]+?))\/?$/i // keys = [{ name: 'bar', optional: false }]

Named parameters

Paths have the ability to define named parameters that populate the keys array. Named parameters are defined by prefixing a colon to a parameter name (:foo) and optionally suffixing a number of different modifiers. A named parameter will match any text until the next slash.

var re = pathToRegexp('/:foo/:bar'); re.exec('/test/route'); //=> ['/test/route', 'test', 'route']

Optional Matches

Named parameters can be suffixed with a question mark to indicate an optional match.

var re = pathToRegExp('/:foo?'); re.exec('/'); //=> ['/', undefined]

Please note: Optional matches can be combined with the greedy match to only have it take effect with the parameter exists. E.g. /:foo*?.

Custom Matching Groups

Named parameters can be provided a custom matching group and override the default. Please note: Backslashes will need to be escaped.

var re = pathToRegexp('/:foo(\\d+)'); re.exec('/123'); //=> ['/123', '123'] re.exec('/abc'); //=> null

Prefixes

By default a named parameter will match any character up until the next slash, but if the parameter is prefixed with a period it will only match to the next period.

var re = pathToRegexp('/test.:foo'); re.exec('/test.json'); //=> ['/test.json', 'json'] re.exec('/test.html.json'); //=> null

Greedy Matching

The path uses an asterisk to greedily match any trailing characters. This can be placed anywhere in the route, including after a named parameter.

var re = pathToRegexp('/foo*'); re.exec('/foo/bar.json'); //=> ['/foo/bar', '/bar.json']

Live Demo

You can see a live demo of this library in use at express-route-tester.

License

MIT

About

Express-style path to regexp

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%