2
votes

I'm using the following regex /^\/(\d{5})$/ in a express route and I'm getting the following error

SyntaxError: Invalid regular expression: /^\/^\/(?(?:([^\/]+?)){5})$\/?$/: Invalid group.

This is the code I'm using:

app.get("/^/(\d{5})$/", function(req, res, next){ console.log(req.params[0]); )};

The regex works just fine when tested on the browser.

Thanks.

2
can you please post your expected output - james jelo4kul
I'm trying to match a 5 digits number, let's say the user send a request like /12345 I'm expecting to get 12345. - Miguel
@Miguel please take a look on my answer - Alvaro Joao

2 Answers

3
votes

The problem in my regex was the use of double quotes, this works:

app.get(/^/(\d{5})$/, function(req, res, next){ console.log(req.params[0]); )};

0
votes

I had time to look to your problem with more cation.

you needed to add an additional \ in \d{5} use like:

app.route('/:id(\\d{5})').get(function(req, res, next){ console.log(req.params.id);});

Pay attention that I setup the name for this parameter as id; you can change that.

http://expressjs.com/api.html