Skip to content

Commit

Permalink
Adding help for how query strings could be handled
Browse files Browse the repository at this point in the history
This is the suggested documentation update that came as part of issue pillarjs#209.
  • Loading branch information
fidian authored Nov 20, 2019
1 parent 0d83ceb commit 2b32daf
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const regexp = pathToRegexp("/foo/:bar", keys);
// keys = [{ name: 'bar', prefix: '/', delimiter: '/', optional: false, repeat: false, pattern: '[^\\/]+?' }]
```

**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).
**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 a parameter as [optional](#optional).

### Parameters

Expand Down Expand Up @@ -138,6 +138,20 @@ regexp.exec("/test/route");

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

When dealing with query strings, escape the question mark (`?`) to ensure it doesn't mark a parameter as optional. Handling unordered query string parameters is outside the scope of this library.

```js
// Example that uses a query string
const regexp = pathToRegexp("/search/:tableName\\?useIndex=true&term=amazing");

regexp.exec("/search/people?useIndex=true&term=amazing");
//=> [ '/search/people?useIndex=true&term=amazing', 'people', index: 0, input: '/search/people?useIndex=true&term=amazing', groups: undefined ]

// This library does not handle query strings in different orders
regexp.exec("/search/people?term=amazing&useIndex=true");
//=> null
```

##### Zero or more

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

0 comments on commit 2b32daf

Please sign in to comment.