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

Do not match certain words as parameters with path-to-regexp #268

Open
Milutin-P opened this issue Dec 16, 2021 · 3 comments
Open

Do not match certain words as parameters with path-to-regexp #268

Milutin-P opened this issue Dec 16, 2021 · 3 comments
Labels

Comments

@Milutin-P
Copy link

Milutin-P commented Dec 16, 2021

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

@tigt
Copy link

tigt commented Mar 7, 2022

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.

@juanmagalhaes
Copy link

+1 here. I'm facing the same issue right now.

@blakeembrey
Copy link
Member

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.

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

No branches or pull requests

5 participants