mirror of
https://github.com/expressjs/expressjs.com.git
synced 2026-02-22 03:51:33 +00:00
docs: clarify req.params type and usage with regular expressions (#2101)
Co-authored-by: krzysdz <krzysdz@users.noreply.github.com>
This commit is contained in:
@@ -8,7 +8,7 @@ console.dir(req.params.name)
|
||||
// => 'tj'
|
||||
```
|
||||
|
||||
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/*`:
|
||||
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/*`:
|
||||
|
||||
```js
|
||||
// GET /file/javascripts/jquery.js
|
||||
@@ -16,6 +16,8 @@ console.dir(req.params[0])
|
||||
// => 'javascripts/jquery.js'
|
||||
```
|
||||
|
||||
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`.
|
||||
|
||||
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.
|
||||
|
||||
Any changes made to the `req.params` object in a middleware or route handler will be reset.
|
||||
|
||||
@@ -8,7 +8,7 @@ console.dir(req.params.name)
|
||||
// => "tj"
|
||||
```
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
```js
|
||||
app.use(/^\/file\/(.*)$/, (req, res) => {
|
||||
@@ -18,6 +18,8 @@ app.use(/^\/file\/(.*)$/, (req, res) => {
|
||||
})
|
||||
```
|
||||
|
||||
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`.
|
||||
|
||||
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.
|
||||
|
||||
Any changes made to the `req.params` object in a middleware or route handler will be reset.
|
||||
|
||||
Reference in New Issue
Block a user