@@ -18,51 +18,50 @@ npm install path-to-regexp --save
18
18
## Usage
19
19
20
20
``` javascript
21
- var pathToRegexp = require (' path-to-regexp' )
21
+ const pathToRegexp = require (' path-to-regexp' )
22
22
23
23
// pathToRegexp(path, keys?, options?)
24
24
// pathToRegexp.parse(path)
25
25
// pathToRegexp.compile(path)
26
26
```
27
27
28
28
- ** path** A string, array of strings, or a regular expression.
29
- - ** keys** An array to be populated with keys found in the path.
29
+ - ** keys** An array to populate with keys found in the path.
30
30
- ** options**
31
31
- ** sensitive** When ` true ` the regexp will be case sensitive. (default: ` false ` )
32
32
- ** strict** When ` true ` the regexp allows an optional trailing delimiter to match. (default: ` false ` )
33
33
- ** end** When ` true ` the regexp will match to the end of the string. (default: ` true ` )
34
34
- ** start** When ` true ` the regexp will match from the beginning of the string. (default: ` true ` )
35
- - Advanced options (use for non-pathname strings, e.g. host names):
36
- - ** delimiter** The default delimiter for segments. (default: ` '/' ` )
37
- - ** endsWith** Optional character, or list of characters, to treat as "end" characters.
38
- - ** delimiters** List of characters to consider delimiters when parsing. (default: ` './' ` )
35
+ - ** delimiter** The default delimiter for segments. (default: ` '/' ` )
36
+ - ** endsWith** Optional character, or list of characters, to treat as "end" characters.
37
+ - ** whitelist** List of characters to consider delimiters when parsing. (default: ` undefined ` , any character)
39
38
40
39
``` javascript
41
- var keys = []
42
- var re = pathToRegexp (' /foo/:bar' , keys)
43
- // re = /^\/foo\/([^\/]+?)\/?$/i
40
+ const keys = []
41
+ const regexp = pathToRegexp (' /foo/:bar' , keys)
42
+ // regexp = /^\/foo\/([^\/]+?)\/?$/i
44
43
// keys = [{ name: 'bar', prefix: '/', delimiter: '/', optional: false, repeat: false, pattern: '[^\\/]+?' }]
45
44
```
46
45
47
- ** Please note:** The ` RegExp ` returned by ` path-to-regexp ` is intended for ordered data (e.g. pathnames, hostnames). It does not handle arbitrary data (e.g. query strings, URL fragments, JSON, etc).
46
+ ** Please note:** The ` RegExp ` returned by ` path-to-regexp ` is intended for ordered data (e.g. pathnames, hostnames). It can not handle arbitrarily ordered data (e.g. query strings, URL fragments, JSON, etc).
48
47
49
48
### Parameters
50
49
51
50
The path argument is used to define parameters and populate the list of keys.
52
51
53
52
#### Named Parameters
54
53
55
- Named parameters are defined by prefixing a colon to the parameter name (` :foo ` ). By default, the parameter will match until the following path segment .
54
+ 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
55
57
56
``` js
58
- var re = pathToRegexp (' /:foo/:bar' )
57
+ const regexp = pathToRegexp (' /:foo/:bar' )
59
58
// keys = [{ name: 'foo', prefix: '/', ... }, { name: 'bar', prefix: '/', ... }]
60
59
61
60
re .exec (' /test/route' )
62
61
// => ['/test/route', 'test', 'route']
63
62
```
64
63
65
- ** Please note:** Parameter names must be made up of "word characters" (` [A-Za-z0-9_] ` ).
64
+ ** Please note:** Parameter names must use "word characters" (` [A-Za-z0-9_] ` ).
66
65
67
66
#### Parameter Modifiers
68
67
@@ -71,7 +70,7 @@ re.exec('/test/route')
71
70
Parameters can be suffixed with a question mark (` ? ` ) to make the parameter optional.
72
71
73
72
``` js
74
- var re = pathToRegexp (' /:foo/:bar?' )
73
+ const regexp = pathToRegexp (' /:foo/:bar?' )
75
74
// keys = [{ name: 'foo', ... }, { name: 'bar', delimiter: '/', optional: true, repeat: false }]
76
75
77
76
re .exec (' /test' )
@@ -81,14 +80,14 @@ re.exec('/test/route')
81
80
// => ['/test', 'test', 'route']
82
81
```
83
82
84
- ** Tip:** If the parameter is the _ only _ value in the segment, the prefix is also optional .
83
+ ** Tip:** The prefix is also optional, escape the prefix ` \/ ` to make it required .
85
84
86
85
##### Zero or more
87
86
88
- Parameters can be suffixed with an asterisk (` * ` ) to denote a zero or more parameter matches. The prefix is taken into account for each match.
87
+ Parameters can be suffixed with an asterisk (` * ` ) to denote a zero or more parameter matches. The prefix is used for each match.
89
88
90
89
``` js
91
- var re = pathToRegexp (' /:foo*' )
90
+ const regexp = pathToRegexp (' /:foo*' )
92
91
// keys = [{ name: 'foo', delimiter: '/', optional: true, repeat: true }]
93
92
94
93
re .exec (' /' )
@@ -100,10 +99,10 @@ re.exec('/bar/baz')
100
99
101
100
##### One or more
102
101
103
- Parameters can be suffixed with a plus sign (` + ` ) to denote a one or more parameter matches. The prefix is taken into account for each match.
102
+ Parameters can be suffixed with a plus sign (` + ` ) to denote a one or more parameter matches. The prefix is used for each match.
104
103
105
104
``` js
106
- var re = pathToRegexp (' /:foo+' )
105
+ const regexp = pathToRegexp (' /:foo+' )
107
106
// keys = [{ name: 'foo', delimiter: '/', optional: false, repeat: true }]
108
107
109
108
re .exec (' /' )
@@ -113,41 +112,50 @@ re.exec('/bar/baz')
113
112
// => ['/bar/baz', 'bar/baz']
114
113
```
115
114
115
+ #### Unnamed Parameters
116
+
117
+ 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.
118
+
119
+ ``` js
120
+ const regexp = pathToRegexp (' /:foo/(.*)' )
121
+ // keys = [{ name: 'foo', ... }, { name: 0, ... }]
122
+
123
+ regexp .exec (' /test/route' )
124
+ // => ['/test/route', 'test', 'route']
125
+ ```
126
+
116
127
#### Custom Matching Parameters
117
128
118
- All parameters can be provided a custom regexp, which overrides the default match (` [^\ /]+ ` ). For example, you can match digits in the path:
129
+ All parameters can have a custom regexp, which overrides the default match (` [^/]+ ` ). For example, you can match digits or names in a path:
119
130
120
131
``` js
121
- var re = pathToRegexp (' /icon-:foo(\\ d+).png' )
132
+ const regexpNumbers = pathToRegexp (' /icon-:foo(\\ d+).png' )
122
133
// keys = [{ name: 'foo', ... }]
123
134
124
- re .exec (' /icon-123.png' )
135
+ regexpNumbers .exec (' /icon-123.png' )
125
136
// => ['/icon-123.png', '123']
126
137
127
- re .exec (' /icon-abc.png' )
138
+ regexpNumbers .exec (' /icon-abc.png' )
128
139
// => null
129
- ```
130
140
131
- ** Please note:** Backslashes need to be escaped with another backslash in strings.
141
+ const regexpWord = pathToRegexp (' /(user|u)' )
142
+ // keys = [{ name: 0, ... }]
132
143
133
- #### Unnamed Parameters
134
-
135
- 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.
136
-
137
- ``` js
138
- var re = pathToRegexp (' /:foo/(.*)' )
139
- // keys = [{ name: 'foo', ... }, { name: 0, ... }]
144
+ regexpWord .exec (' /u' )
145
+ // => ['/u', 'u']
140
146
141
- re .exec (' /test/route ' )
142
- // => ['/test/route', 'test', 'route']
147
+ regexpWord .exec (' /users ' )
148
+ // => null
143
149
```
144
150
151
+ ** Tip:** Backslashes need to be escaped with another backslash in JavaScript strings.
152
+
145
153
### Parse
146
154
147
155
The parse function is exposed via ` pathToRegexp.parse ` . This will return an array of strings and keys.
148
156
149
157
``` js
150
- var tokens = pathToRegexp .parse (' /route/:foo/(.*)' )
158
+ const tokens = pathToRegexp .parse (' /route/:foo/(.*)' )
151
159
152
160
console .log (tokens[0 ])
153
161
// => "/route"
@@ -166,7 +174,7 @@ console.log(tokens[2])
166
174
Path-To-RegExp exposes a compile function for transforming a string into a valid path.
167
175
168
176
``` js
169
- var toPath = pathToRegexp .compile (' /user/:id' )
177
+ const toPath = pathToRegexp .compile (' /user/:id' )
170
178
171
179
toPath ({ id: 123 }) // => "/user/123"
172
180
toPath ({ id: ' café' }) // => "/user/caf%C3%A9"
@@ -175,12 +183,12 @@ toPath({ id: '/' }) //=> "/user/%2F"
175
183
toPath ({ id: ' :/' }) // => "/user/%3A%2F"
176
184
toPath ({ id: ' :/' }, { encode : (value , token ) => value }) // => "/user/:/"
177
185
178
- var toPathRepeated = pathToRegexp .compile (' /:segment+' )
186
+ const toPathRepeated = pathToRegexp .compile (' /:segment+' )
179
187
180
188
toPathRepeated ({ segment: ' foo' }) // => "/foo"
181
189
toPathRepeated ({ segment: [' a' , ' b' , ' c' ] }) // => "/a/b/c"
182
190
183
- var toPathRegexp = pathToRegexp .compile (' /user/:id(\\ d+)' )
191
+ const toPathRegexp = pathToRegexp .compile (' /user/:id(\\ d+)' )
184
192
185
193
toPathRegexp ({ id: 123 }) // => "/user/123"
186
194
toPathRegexp ({ id: ' 123' }) // => "/user/123"
@@ -199,11 +207,10 @@ Path-To-RegExp exposes the two functions used internally that accept an array of
199
207
#### Token Information
200
208
201
209
* ` name ` The name of the token (` string ` for named or ` number ` for index)
202
- * ` prefix ` The prefix character for the segment (` / ` or ` . ` )
203
- * ` delimiter ` The delimiter for the segment (same as prefix or ` / ` )
210
+ * ` prefix ` The prefix character for the segment (e.g. ` / ` )
211
+ * ` delimiter ` The delimiter for the segment (same as prefix or default delimiter )
204
212
* ` optional ` Indicates the token is optional (` boolean ` )
205
213
* ` repeat ` Indicates the token is repeated (` boolean ` )
206
- * ` partial ` Indicates this token is a partial path segment (` boolean ` )
207
214
* ` pattern ` The RegExp used to match this token (` string ` )
208
215
209
216
## Compatibility with Express <= 4.x
0 commit comments