3
3
> Turn a path string such as ` /user/:name ` into a regular expression.
4
4
5
5
[ ![ NPM version] [ npm-image ]] [ npm-url ]
6
- [ ![ Build status ] [ travis -image]] [ travis -url]
7
- [ ![ Test coverage ] [ coveralls -image]] [ coveralls -url]
8
- [ ![ Dependency Status ] [ david -image]] [ david -url]
6
+ [ ![ NPM downloads ] [ downloads -image]] [ downloads -url]
7
+ [ ![ Build status ] [ build -image]] [ build -url]
8
+ [ ![ Build coverage ] [ coverage -image]] [ coverage -url]
9
9
[ ![ License] [ license-image ]] [ license-url ]
10
- [ ![ Downloads] [ downloads-image ]] [ downloads-url ]
11
10
12
11
## Installation
13
12
@@ -26,9 +25,13 @@ const { pathToRegexp, match, parse, compile } = require("path-to-regexp");
26
25
// compile(path)
27
26
```
28
27
28
+ ### Path to regexp
29
+
30
+ The ` pathToRegexp ` function will return a regular expression object based on the provided ` path ` argument. It accepts the following arguments:
31
+
29
32
- ** path** A string, array of strings, or a regular expression.
30
- - ** keys** An array to populate with keys found in the path.
31
- - ** options**
33
+ - ** keys** _ (optional) _ An array to populate with keys found in the path.
34
+ - ** options** _ (optional) _
32
35
- ** sensitive** When ` true ` the regexp will be case sensitive. (default: ` false ` )
33
36
- ** strict** When ` true ` the regexp won't allow an optional trailing delimiter to match. (default: ` false ` )
34
37
- ** end** When ` true ` the regexp will match to the end of the string. (default: ` true ` )
@@ -194,6 +197,23 @@ fn("/invalid"); //=> false
194
197
fn (" /user/caf%C3%A9" ); // => { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } }
195
198
```
196
199
200
+ The ` match ` function can be used to custom match named parameters. For example, this can be used to whitelist a small number of valid paths:
201
+
202
+ ``` js
203
+ const urlMatch = match (" /users/:id/:tab(home|photos|bio)" , {
204
+ decode: decodeURIComponent,
205
+ });
206
+
207
+ urlMatch (" /users/1234/photos" );
208
+ // => { path: '/users/1234/photos', index: 0, params: { id: '1234', tab: 'photos' } }
209
+
210
+ urlMatch (" /users/1234/bio" );
211
+ // => { path: '/users/1234/bio', index: 0, params: { id: '1234', tab: 'bio' } }
212
+
213
+ urlMatch (" /users/1234/otherstuff" );
214
+ // => false
215
+ ```
216
+
197
217
#### Process Pathname
198
218
199
219
You should make sure variations of the same path match the expected ` path ` . Here's one possible solution using ` encode ` :
@@ -321,15 +341,13 @@ You can see a live demo of this library in use at [express-route-tester](http://
321
341
322
342
MIT
323
343
324
- [ npm-image ] : https://img.shields.io/npm/v/path-to-regexp.svg?style=flat
344
+ [ npm-image ] : https://img.shields.io/npm/v/path-to-regexp
325
345
[ npm-url ] : https://npmjs.org/package/path-to-regexp
326
- [ travis -image] : https://img.shields.io/travis/pillarjs /path-to-regexp.svg?style=flat
327
- [ travis -url] : https://travis-ci .org/pillarjs /path-to-regexp
328
- [ coveralls -image] : https://img.shields.io/coveralls/ pillarjs/path-to-regexp.svg?style=flat
329
- [ coveralls -url] : https://coveralls.io/r/ pillarjs/path-to-regexp?branch=master
330
- [ david -image] : http ://img.shields.io/david/ pillarjs/path-to-regexp.svg?style=flat
331
- [ david -url] : https://david-dm.org /pillarjs/path-to-regexp
346
+ [ downloads -image] : https://img.shields.io/npm/dm /path-to-regexp
347
+ [ downloads -url] : https://npmjs .org/package /path-to-regexp
348
+ [ build -image] : https://img.shields.io/github/workflow/status/ pillarjs/path-to-regexp/CI/master
349
+ [ build -url] : https://github.com/ pillarjs/path-to-regexp/actions/workflows/ci.yml?query=branch%3Amaster
350
+ [ coverage -image] : https ://img.shields.io/codecov/c/gh/ pillarjs/path-to-regexp
351
+ [ coverage -url] : https://codecov.io/gh /pillarjs/path-to-regexp
332
352
[ license-image ] : http://img.shields.io/npm/l/path-to-regexp.svg?style=flat
333
353
[ license-url ] : LICENSE.md
334
- [ downloads-image ] : http://img.shields.io/npm/dm/path-to-regexp.svg?style=flat
335
- [ downloads-url ] : https://npmjs.org/package/path-to-regexp
0 commit comments