Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match paths that don't match a specific pattern: Express Router

I want to ignore all paths of the form /foo/* on my express server. So I want to do

app.get('someRegexThatDoesntMatchFoo/*', routes.index)

I have tried

app.get('/^\(\(.*foo/.*\)\@!.\)*$', routes.index);

but that didnt work-- it doesn't catch all-routes-besides-foo and apply routes.index instead i get a CANNOT GET bar for any /bar request

Any suggestions?

Thanks!

like image 537
algorithmicCoder Avatar asked Dec 25 '22 21:12

algorithmicCoder


1 Answers

The first answer was not correct, that's why I post again.

Solution

The following regex will match any path except those starting with /foo/

app.get(/^\/([^f]|f[^o]|fo[^o]|foo[^/]).*$/, routes.index);

This solution gets more and more complex as the size of the string increases.

Recommended

Anyway, looking here for a regex is not the right thing.

When configuring routes, you have always to start with the more special rule and finish with the most general. Like this you would not run in such issues.

You first have to define your route for /foo/* and after that for all others *.

like image 84
Lorenz Meyer Avatar answered Dec 28 '22 10:12

Lorenz Meyer