Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex parameters #127

Closed
punmechanic opened this issue May 23, 2015 · 5 comments
Closed

Regex parameters #127

punmechanic opened this issue May 23, 2015 · 5 comments

Comments

@punmechanic
Copy link

Is there any support for parameter matching using regular expressions? For example, something like this:

router.get(/^foo\/(?param[A-z]*)\/baz$/, function *(param) {
  console.log(param);
});

foo/qux/baz would then print out qux to the console, but foo/0/baz would be ignored.

@alexmingoia
Copy link
Collaborator

Regular expressions are not supported. Supporting them makes diffing and concatenation of route paths too complex (used for nesting routers, prefixing, etc.). Also, in the future I plan on optimizing the route matching by using a trie hash, at which point regex's won't be used internally at all.

I suggest using .param() for parameter validation:

router
  .param('bar', function *(bar, next) {
    if (!bar.match(/[A-z]+/)) {
      return this.status = 404;
    }
    yield next;
  })
  .get('/foo/:bar/baz')

@hbakhtiyor
Copy link

hi @alexmingoia, still not supported?

@punmechanic
Copy link
Author

@hbakhtiyor Looking back on this request I think it wasn't a great idea. There's almost certainly a better way to achieve what you want to do. What problem do you need to solve that requires regex routes?

@hbakhtiyor
Copy link

a bit like yours, but make some param is optional
e.g.
foo/qux.baz and make .baz is optional

@punmechanic
Copy link
Author

punmechanic commented Oct 23, 2018

This project uses path-to-regexp for its route matching, so you can use optional parameters without regex.

foo/qux.:paramName(allowedValue|allowedValue2)?

https://github.com/pillarjs/path-to-regexp

This issue should remain closed as the router itself doesn't apply much special logic to paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants