Skip to content

Commit ae9e576

Browse files
authored
New example in documentation (#256)
1 parent 77df638 commit ae9e576

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Readme.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ const { pathToRegexp, match, parse, compile } = require("path-to-regexp");
2626
// compile(path)
2727
```
2828

29+
### Path to regexp
30+
31+
The `pathToRegexp` function will return a regular expression object based on the provided `path` argument. It accepts the following arguments:
32+
2933
- **path** A string, array of strings, or a regular expression.
30-
- **keys** An array to populate with keys found in the path.
31-
- **options**
34+
- **keys** _(optional)_ An array to populate with keys found in the path.
35+
- **options** _(optional)_
3236
- **sensitive** When `true` the regexp will be case sensitive. (default: `false`)
3337
- **strict** When `true` the regexp won't allow an optional trailing delimiter to match. (default: `false`)
3438
- **end** When `true` the regexp will match to the end of the string. (default: `true`)
@@ -194,6 +198,21 @@ fn("/invalid"); //=> false
194198
fn("/user/caf%C3%A9"); //=> { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } }
195199
```
196200

201+
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:
202+
203+
```js
204+
const urlMatch = match("/users/:id/:tab(home|photos|bio)", { decode: decodeURIComponent });
205+
206+
urlMatch("/users/1234/photos")
207+
//=> { path: '/users/1234/photos', index: 0, params: { id: '1234', tab: 'photos' } }
208+
209+
urlMatch("/users/1234/bio")
210+
//=> { path: '/users/1234/bio', index: 0, params: { id: '1234', tab: 'bio' } }
211+
212+
urlMatch("/users/1234/otherstuff")
213+
//=> false
214+
```
215+
197216
#### Process Pathname
198217

199218
You should make sure variations of the same path match the expected `path`. Here's one possible solution using `encode`:

0 commit comments

Comments
 (0)