Skip to content

Commit 6c91e0c

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

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
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 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
1515
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ 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 keys using `req.params[n]`, where `n` is the n<sup>th</sup> capture group.
1212

1313
```js
1414
app.use(/^\/file\/(.*)$/, (req, res) => {
@@ -18,6 +18,8 @@ app.use(/^\/file\/(.*)$/, (req, res) => {
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)