Skip to content

Commit 18b7bdd

Browse files
committed
docs: clarify req.params type and usage with regular expressions
Fixes #2100
1 parent 738f860 commit 18b7bdd

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

_includes/api/en/4x/req-params.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ console.dir(req.params.name)
88
// => 'tj'
99
```
1010

11-
When you use a regular expression for the route definition, capture groups are provided in the array using `req.params[n]`, where `n` is the n<sup>th</sup> capture group. This rule is applied to unnamed wild card matches with string routes such as `/file/*`:
11+
When you use a regular expression for the route definition, capture groups are provided as integer string keys using `req.params['n']`, where `n` is the n<sup>th</sup> capture group. This rule is applied to unnamed wild card matches with string routes such as `/file/*`:
1212

1313
```js
1414
// GET /file/javascripts/jquery.js
15-
console.dir(req.params[0])
15+
console.dir(req.params['0'])
1616
// => 'javascripts/jquery.js'
1717
```
1818

19+
Named capturing groups in regular expressions behave like named route parameters. For example the group from `/^\/file\/(?<path>.*)$/` expression is available as `req.params.path`.
20+
1921
If you need to make changes to a key in `req.params`, use the [app.param](/{{ page.lang }}/4x/api.html#app.param) handler. Changes are applicable only to [parameters](/{{ page.lang }}/guide/routing.html#route-parameters) already defined in the route path.
2022

2123
Any changes made to the `req.params` object in a middleware or route handler will be reset.

_includes/api/en/5x/req-params.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ console.dir(req.params.name)
88
// => "tj"
99
```
1010

11-
When you use a regular expression for the route definition, capture groups are provided in the array using `req.params[n]`, where `n` is the n<sup>th</sup> capture group.
11+
When you use a regular expression for the route definition, capture groups are provided as integer string keys using `req.params['n']`, where `n` is the n<sup>th</sup> capture group.
1212

1313
```js
1414
app.use(/^\/file\/(.*)$/, (req, res) => {
1515
// GET /file/javascripts/jquery.js
16-
console.dir(req.params[0])
16+
console.dir(req.params['0'])
1717
// => "javascripts/jquery.js"
1818
})
1919
```
2020

21+
Named capturing groups in regular expressions behave like named route parameters. For example the group from `/^\/file\/(?<path>.*)$/` expression is available as `req.params.path`.
22+
2123
If you need to make changes to a key in `req.params`, use the [app.param](/{{ page.lang }}/5x/api.html#app.param) handler. Changes are applicable only to [parameters](/{{ page.lang }}/guide/routing.html#route-parameters) already defined in the route path.
2224

2325
Any changes made to the `req.params` object in a middleware or route handler will be reset.

0 commit comments

Comments
 (0)