path-to-regexp
path-to-regexp copied to clipboard
Do not match certain words as parameters with path-to-regexp
I have a route /:parameter
and if someone pings that route I should serve him certain page (React App)
So what I need is some kind of regexp that wont match certain words...
Regular expression should be ^(?!.*(foobar1|\/foobar2)).*$
https://pshrmn.github.io/route-tester/#/ I've used this tool for testing and entered various combinations, such as /parameter(?!.*(foobar1|\/foobar2))
but no luck...
So test case should be
if path is /foobar3
it should return page
if path is /foobar2
or /foobar3
it should return null
Does “the best regex trick” work for your needs? I think it would express your example as /(?:foobar2|foobar3|(foobar1))/
, but I admit I couldn’t figure out how to translate it into a form that the linked route-tester page was happy with.
+1 here. I'm facing the same issue right now.
If someone wants to propose expanding the existing syntax to support this, I'm happy to review.
Otherwise I'd suggest maybe filing a ticket with the libraries using this package to support this feature. It's definitely easier than trying to construct a negative regex. For example, in something like Express you can have middleware that runs and write a line of code that says parameter !== 'foobar1'
to skip the middleware on that condition.