Apply regular expression for expressjs
Problem
I need to handle requests like this:
/key/6435.254.53538
I wrote regexp that work in RegexPal
(([0-9]+\.?)+)
I create handler in Express.js
/key/:key(([0-9]+\.?)+)
but it returns only last part of the key
req.params.key == '53538'
how can I retrieve full key?
Problem courtesy of: farlenkov
Solution
The problem seems to be that you accidently capture the digits, and only the last capture is returned. Try either:
/key/:key((?:[0-9]+\.?)+)
or
/key/:key(((?:[0-9]+\.?)+))
Alternatively:
/key/:key([\d.]+)
The only difference is that it allows leading and consecutive periods. If you know that will never be the case, or not a problem, then this regex is a lot easier to read.
Solution courtesy of: Martin Ender
Discussion
View additional discussion.