For example, can I make a regex so that the string passes if it does not contain "foo"?
What I'm doing is using nodejs express to match URLs. How do I match any URL that does not contain "foo"?
For example, the following matches the root page:
app.get('/', function(req, res){
res.send('hello world');
});
But how can I do the same to match routes except for /login
?
app.get(/regex_here/, function(req, res){
res.send('hello world');
});
What should be the value of regex_here
to match all pages except /login
?
From the documentation, it seems like you must use regular expressions.
So you will need to go with nhahtdh's pattern:
/^(?!.*foo).*$/
What does
/^(?!.*foo).*$/
mean exactly?
The parenthesized expression is called a negative lookahead, i.e. the pattern can only match when, .*foo
doesn't follow immediately after the current position, which is the start of the string, as denoted by ^
.
So, the pattern matches everything—.*
, unless foo
is found somewhere after the beginning, which is to say the same as:
The pattern matches everything unless `foo` occurs in the string somewhere.