@@ -33,10 +33,10 @@ const { pathToRegexp, match, parse, compile } = require("path-to-regexp");
33
33
- ** strict** When ` true ` the regexp allows an optional trailing delimiter to match. (default: ` false ` )
34
34
- ** end** When ` true ` the regexp will match to the end of the string. (default: ` true ` )
35
35
- ** start** When ` true ` the regexp will match from the beginning of the string. (default: ` true ` )
36
- - ** delimiter** The default delimiter for segments. (default: ` '/' ` )
36
+ - ** delimiter** The default delimiter for segments, e.g. ` [^/] ` for ` :named ` patterns . (default: ` '/' ` )
37
37
- ** endsWith** Optional character, or list of characters, to treat as "end" characters.
38
- - ** whitelist** List of characters to consider delimiters when parsing. (default: ` undefined ` , any character)
39
38
- ** encode** A function to encode strings before inserting into ` RegExp ` . (default: ` x => x ` )
39
+ - ** prefixes** List of characters to automatically consider prefixes when parsing. (default: ` ./ ` )
40
40
41
41
``` javascript
42
42
const keys = [];
@@ -49,11 +49,11 @@ const regexp = pathToRegexp("/foo/:bar", keys);
49
49
50
50
### Parameters
51
51
52
- The path argument is used to define parameters and populate the list of keys.
52
+ The path argument is used to define parameters and populate keys.
53
53
54
54
#### Named Parameters
55
55
56
- Named parameters are defined by prefixing a colon to the parameter name (` :foo ` ). By default, the parameter will match until the next prefix (e.g. ` [^/]+ ` ).
56
+ Named parameters are defined by prefixing a colon to the parameter name (` :foo ` ).
57
57
58
58
``` js
59
59
const regexp = pathToRegexp (" /:foo/:bar" );
@@ -65,7 +65,61 @@ regexp.exec("/test/route");
65
65
66
66
** Please note:** Parameter names must use "word characters" (` [A-Za-z0-9_] ` ).
67
67
68
- #### Parameter Modifiers
68
+ ##### Custom Matching Parameters
69
+
70
+ Parameters can have a custom regexp, which overrides the default match (` [^/]+ ` ). For example, you can match digits or names in a path:
71
+
72
+ ``` js
73
+ const regexpNumbers = pathToRegexp (" /icon-:foo(\\ d+).png" );
74
+ // keys = [{ name: 'foo', ... }]
75
+
76
+ regexpNumbers .exec (" /icon-123.png" );
77
+ // => ['/icon-123.png', '123']
78
+
79
+ regexpNumbers .exec (" /icon-abc.png" );
80
+ // => null
81
+
82
+ const regexpWord = pathToRegexp (" /(user|u)" );
83
+ // keys = [{ name: 0, ... }]
84
+
85
+ regexpWord .exec (" /u" );
86
+ // => ['/u', 'u']
87
+
88
+ regexpWord .exec (" /users" );
89
+ // => null
90
+ ```
91
+
92
+ ** Tip:** Backslashes need to be escaped with another backslash in JavaScript strings.
93
+
94
+ ##### Custom Prefix and Suffix
95
+
96
+ Parameters can be wrapped in ` {} ` to create custom prefixes or suffixes for your segment:
97
+
98
+ ``` js
99
+ const regexp = pathToRegexp (" /:attr1?{-:attr2}?{-:attr3}?" );
100
+
101
+ regexp .exec (" /test" );
102
+ // => ['/test', 'test', undefined, undefined]
103
+
104
+ regexp .exec (" /test-test" );
105
+ // => ['/test', 'test', 'test', undefined]
106
+ ```
107
+
108
+ #### Unnamed Parameters
109
+
110
+ It is possible to write an unnamed parameter that only consists of a regexp. It works the same the named parameter, except it will be numerically indexed:
111
+
112
+ ``` js
113
+ const regexp = pathToRegexp (" /:foo/(.*)" );
114
+ // keys = [{ name: 'foo', ... }, { name: 0, ... }]
115
+
116
+ regexp .exec (" /test/route" );
117
+ // => [ '/test/route', 'test', 'route', index: 0, input: '/test/route', groups: undefined ]
118
+ ```
119
+
120
+ #### Modifiers
121
+
122
+ Modifiers must be placed after the parameter (e.g. ` /:foo? ` , ` /(test)? ` , or ` /:foo(test)? ` ).
69
123
70
124
##### Optional
71
125
@@ -86,7 +140,7 @@ regexp.exec("/test/route");
86
140
87
141
##### Zero or more
88
142
89
- Parameters can be suffixed with an asterisk (` * ` ) to denote a zero or more parameter matches. The prefix is used for each match.
143
+ Parameters can be suffixed with an asterisk (` * ` ) to denote a zero or more parameter matches.
90
144
91
145
``` js
92
146
const regexp = pathToRegexp (" /:foo*" );
@@ -101,7 +155,7 @@ regexp.exec("/bar/baz");
101
155
102
156
##### One or more
103
157
104
- Parameters can be suffixed with a plus sign (` + ` ) to denote a one or more parameter matches. The prefix is used for each match.
158
+ Parameters can be suffixed with a plus sign (` + ` ) to denote a one or more parameter matches.
105
159
106
160
``` js
107
161
const regexp = pathToRegexp (" /:foo+" );
@@ -114,44 +168,6 @@ regexp.exec("/bar/baz");
114
168
// => [ '/bar/baz','bar/baz', index: 0, input: '/bar/baz', groups: undefined ]
115
169
```
116
170
117
- #### Unnamed Parameters
118
-
119
- It is possible to write an unnamed parameter that only consists of a matching group. It works the same as a named parameter, except it will be numerically indexed.
120
-
121
- ``` js
122
- const regexp = pathToRegexp (" /:foo/(.*)" );
123
- // keys = [{ name: 'foo', ... }, { name: 0, ... }]
124
-
125
- regexp .exec (" /test/route" );
126
- // => [ '/test/route', 'test', 'route', index: 0, input: '/test/route', groups: undefined ]
127
- ```
128
-
129
- #### Custom Matching Parameters
130
-
131
- All parameters can have a custom regexp, which overrides the default match (` [^/]+ ` ). For example, you can match digits or names in a path:
132
-
133
- ``` js
134
- const regexpNumbers = pathToRegexp (" /icon-:foo(\\ d+).png" );
135
- // keys = [{ name: 'foo', ... }]
136
-
137
- regexpNumbers .exec (" /icon-123.png" );
138
- // => ['/icon-123.png', '123']
139
-
140
- regexpNumbers .exec (" /icon-abc.png" );
141
- // => null
142
-
143
- const regexpWord = pathToRegexp (" /(user|u)" );
144
- // keys = [{ name: 0, ... }]
145
-
146
- regexpWord .exec (" /u" );
147
- // => ['/u', 'u']
148
-
149
- regexpWord .exec (" /users" );
150
- // => null
151
- ```
152
-
153
- ** Tip:** Backslashes need to be escaped with another backslash in JavaScript strings.
154
-
155
171
### Match
156
172
157
173
The ` match ` function will return a function for transforming paths into parameters:
0 commit comments