Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Apply regular expression for expressjs

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.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Apply regular expression for expressjs

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×