From 2b32daf1fe9eb5d89af03516b7fd4fe3e6946a9b Mon Sep 17 00:00:00 2001 From: Tyler Akins Date: Wed, 20 Nov 2019 12:54:28 -0600 Subject: [PATCH 1/2] Adding help for how query strings could be handled This is the suggested documentation update that came as part of issue #209. --- Readme.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index bdaf62d..425fe52 100644 --- a/Readme.md +++ b/Readme.md @@ -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 @@ -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. From b1704337886862e6606aec1161fe94aff07a01ce Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Wed, 20 Nov 2019 11:17:46 -0800 Subject: [PATCH 2/2] Tweaks --- Readme.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index 425fe52..4c5e47f 100644 --- a/Readme.md +++ b/Readme.md @@ -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). 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). +**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). ### Parameters @@ -138,10 +138,9 @@ 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. +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. ```js -// Example that uses a query string const regexp = pathToRegexp("/search/:tableName\\?useIndex=true&term=amazing"); regexp.exec("/search/people?useIndex=true&term=amazing");