10000 Document query string behavior (#210) · pillarjs/path-to-regexp@43e87bf · GitHub
[go: up one dir, main page]

Skip to content

Commit 43e87bf

Browse files
fidianblakeembrey
authored andcommitted
Document query string behavior (#210)
1 parent 2ea948c commit 43e87bf

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Readme.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const regexp = pathToRegexp("/foo/:bar", keys);
4545
// keys = [{ name: 'bar', prefix: '/', delimiter: '/', optional: false, repeat: false, pattern: '[^\\/]+?' }]
4646
```
4747

48-
**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+
**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).
4949

5050
### Parameters
5151

@@ -138,6 +138,19 @@ regexp.exec("/test/route");
138138

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

141+
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.
142+
143+
```js
144+
const regexp = pathToRegexp("/search/:tableName\\?useIndex=true&term=amazing");
145+
146+
regexp.exec("/search/people?useIndex=true&term=amazing");
147+
//=> [ '/search/people?useIndex=true&term=amazing', 'people', index: 0, input: '/search/people?useIndex=true&term=amazing', groups: undefined ]
148+
149+
// This library does not handle query strings in different orders
150+
regexp.exec("/search/people?term=amazing&useIndex=true");
151+
//=> null
152+
```
153+
141154
##### Zero or more
142155

143156
Parameters can be suffixed with an asterisk (`*`) to denote a zero or more parameter matches.

0 commit comments

Comments
 (0)
0