@@ -26,9 +26,13 @@ const { pathToRegexp, match, parse, compile } = require("path-to-regexp");
26
26
// compile(path)
27
27
```
28
28
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
+
29
33
- ** 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) _
32
36
- ** sensitive** When ` true ` the regexp will be case sensitive. (default: ` false ` )
33
37
- ** strict** When ` true ` the regexp won't allow an optional trailing delimiter to match. (default: ` false ` )
34
38
- ** end** When ` true ` the regexp will match to the end of the string. (default: ` true ` )
@@ -194,6 +198,21 @@ fn("/invalid"); //=> false
194
198
fn (" /user/caf%C3%A9" ); // => { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } }
195
199
```
196
200
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
+
197
216
#### Process Pathname
198
217
199
218
You should make sure variations of the same path match the expected ` path ` . Here's one possible solution using ` encode ` :
0 commit comments