Skip to content

Commit 30f03fa

Browse files
committed
Document query string behavior
1 parent 803274e commit 30f03fa

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

README.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ regexp := pathToRegexp.Must(pathToRegexp.PathToRegexp("/foo/:bar", &tokens, nil)
6868
// tokens: [{Name:"bar", Prefix:"/", Suffix:"", Pattern:"[^\\/]+?", Modifier:""}]
6969
```
7070

71-
**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).
71+
**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). When using paths that contain query strings, you need to escape the question mark (`?`) to ensure it does not flag the parameter as [optional](#optional).
7272

7373
### Parameters
7474

@@ -187,14 +187,14 @@ regexp := pathToRegexp.Must(pathToRegexp.PathToRegexp("/:foo/:bar?", nil, nil))
187187
// {Name:"bar", Prefix:"/", Suffix:"", Pattern:"[^\\/]+?", Modifier:"?"}
188188
// ]
189189

190-
match, err := regexp.FindStringMatch("/test")
190+
match, _ := regexp.FindStringMatch("/test")
191191
for _, g := range match.Groups() {
192192
fmt.Printf("%q ", g.String())
193193
}
194194
fmt.Printf("%d %q\n", match.Index, match)
195195
//=> "/test" "test" "" 0 "/test"
196196

197-
match, err = regexp.FindStringMatch("/test/route")
197+
match, _ = regexp.FindStringMatch("/test/route")
198198
for _, g := range match.Groups() {
199199
fmt.Printf("%q ", g.String())
200200
}
@@ -204,6 +204,24 @@ fmt.Printf("%d %q\n", match.Index, match)
204204

205205
**Tip:** The prefix is also optional, escape the prefix `\/` to make it required.
206206

207+
When dealing with query strings, escape the question mark (`?`) so it doesn't mark the parameter as optional. Handling unordered data is outside the scope of this library.
208+
209+
```go
210+
regexp := pathToRegexp.Must(pathToRegexp.PathToRegexp("/search/:tableName\\?useIndex=true&term=amazing", nil, nil))
211+
212+
match, _ := regexp.FindStringMatch("/search/people?useIndex=true&term=amazing")
213+
for _, g := range match.Groups() {
214+
fmt.Printf("%q ", g.String())
215+
}
216+
fmt.Printf("%d %q\n", match.Index, match)
217+
//=> "/search/people?useIndex=true&term=amazing" "people" 0 "/search/people?useIndex=true&term=amazing"
218+
219+
// This library does not handle query strings in different orders
220+
match, _ = regexp.FindStringMatch("/search/people?term=amazing&useIndex=true")
221+
fmt.Println(match)
222+
//=> <nil>
223+
```
224+
207225
##### Zero or more
208226

209227
Parameters can be suffixed with an asterisk (`*`) to denote a zero or more parameter matches.
@@ -212,14 +230,14 @@ Parameters can be suffixed with an asterisk (`*`) to denote a zero or more param
212230
regexp := pathToRegexp.Must(pathToRegexp.PathToRegexp("/:foo*", nil, nil))
213231
// tokens: [{Name:"foo", Prefix:"/", Suffix:"", Pattern:"[^\\/]+?", Modifier:"*"}]
214232

215-
match, err := regexp.FindStringMatch("/")
233+
match, _ := regexp.FindStringMatch("/")
216234
for _, g := range match.Groups() {
217235
fmt.Printf("%q ", g.String())
218236
}
219237
fmt.Printf("%d %q\n", match.Index, match)
220238
//=> "/" "" 0 "/"
221239

222-
match, err = regexp.FindStringMatch("/bar/baz")
240+
match, _ = regexp.FindStringMatch("/bar/baz")
223241
for _, g := range match.Groups() {
224242
fmt.Printf("%q ", g.String())
225243
}
@@ -235,11 +253,11 @@ Parameters can be suffixed with a plus sign (`+`) to denote a one or more parame
235253
regexp := pathToRegexp.Must(pathToRegexp.PathToRegexp("/:foo+", nil, nil))
236254
// tokens: [{Name:"foo", Prefix:"/", Suffix:"", Pattern:"[^\\/]+?", Modifier:"+"}]
237255

238-
match, err := regexp.FindStringMatch("/")
256+
match, _ := regexp.FindStringMatch("/")
239257
fmt.Println(match)
240258
//=> nil
241259

242-
match, err = regexp.FindStringMatch("/bar/baz")
260+
match, _ = regexp.FindStringMatch("/bar/baz")
243261
for _, g := range match.Groups() {
244262
fmt.Printf("%q ", g.String())
245263
}

0 commit comments

Comments
 (0)