Question

what dose this mean When you use a regular expression for the route definition, capture groups are provided in the array using req.params[n]

Hi i have been reading the express js docs and i got this sentence but i dont understand what dose this means this sentense is :

When you use a regular expression for the route definition, capture groups are provided in the array using req.params[n]

please can you give me an example that clarifay this sentense for me

the link in docs is : https://expressjs.com/en/4x/api.html#req.params


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
November 21, 2022

Hi there,

Let’s say that you have the following route: /file/*, this means that anything after the /file/ will match the * wildcard regex.

So if you receive a GET request to /file/javascripts/jquery.js, in order to access the parameter you can use a standard array syntax as follows:

console.dir(req.params[0])

And the output in this case will be:

// => 'javascripts/jquery.js'

Hope that this helps!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up