regexcoldfusionfw1

FW/1 pattern matching N digits


I am trying to match routes where IDs have exactly 6 numbers

This does not work:

variables.framework.routes = [
    { "main/{id:[0-9]{6}}"  = "main/home/eid/:id"},
    { "main/home"                   = "main/home"},
    { "*"                           = "main/404"}
    ];

This does:

variables.framework.routes = [
    { "main/{id:[0-9]+}"    = "main/home/eid/:id"},
    { "main/home"                   = "main/home"},
    { "*"                           = "main/404"}
    ];

The second one of course matches on any number of digits. I wonder if I have to escape the {


Solution

  • It looks like FW/1 only allows a limited regular expression syntax for the routes declaration. So I don't think your first example will work. From what I could find the limited regular expression syntax in routes was added to FW/1 version 3.5. I found some discussion on the topic and this specific comment describing the requested behavior - https://github.com/framework-one/fw1/issues/325#issuecomment-118572702

    {placeholder:regex}, so we could have product/{id:[0-9]+}-:name.html that targets product.detail?id={id:[0-9]+}&name=:name.

    • You need to repeat the placeholder with the regex in the target route too (could be changed).
    • You can't put } in your placeholder specific regex.

    Let me know if a PR is welcome for this add-on.

    Notice that second bullet point which mentions that the } (bracket) is not allowed in the placeholder regex.

    Here is a link to the code referenced by that pull-request which was included in 3.5 - https://github.com/framework-one/fw1/commit/9543b78552dbd27a526083ac72a3846bd86eeb90

    And here is a link to the updated documentation for version 3.5 where some information was added about this feature - http://framework-one.github.io/documentation/developing-applications.html#url-routes

    Snippet of that doc here:

    Placeholder variables in the route are identified either by a leading colon or by braces (specifying a variable name and a regex to restrict matches) and can appear in the URL as well, for example { "/product/:id" = "/product/view/id/:id" } specifies a match for /product/something which will be treated as if the URL was /product/view/id/something - section: product, item: view, query string id=something. Similarly, { "/product/{id:[0-9]+}" = "/product/view/id/:id" } specifies a match for /product/42 which will be treated as if the URL was /product/view/id/42, and only numeric values will match the placeholder.