pillarjs / path-to-regexp

Turn a path string such as `/user/:name` into a regular expression

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom parameter length

tgbv opened this issue · comments

How to match a route of which parameter must have a certain length? For instance, "locale" must be exactly two characters and the following expression doesn't match:

/route/:locale({2})/something-else

Hey, @tgbv.

You can use a regular expression group to specify the length of the parameter:

var { match } = require("path-to-regexp")

const fn = match('/route/(.{2})/foo')
fn('/route/en/foo')
// {path: "/route/en/foo", index: 0, params: Object {0: "en"}}

To my best knowledge, path parameters don't support the exact length notation. The "locale" parameter will be available as the first (0) unnamed parameter.