path-to-regexp icon indicating copy to clipboard operation
path-to-regexp copied to clipboard

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

Open Milutin-P opened this issue 3 years ago • 3 comments

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

Milutin-P avatar Dec 16 '21 12:12 Milutin-P

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.

tigt avatar Mar 07 '22 02:03 tigt

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

juanmagalhaes avatar Nov 30 '22 18:11 juanmagalhaes

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.

blakeembrey avatar Dec 01 '22 01:12 blakeembrey