1

I have a regular expression that matches strings that start with either the word "db" or the word "admin":

/^\/(db|admin)\//

I'm refactoring my code and my requirements have changed: Now I need the opposite, i.e. a regular expression that matches strings that don't start with one of those two words. Is this possible with regular expressions?

Note: I cannot use JS API - the regular expression is inserted in Express.js's app.all(path, callback) method directly (as the path).

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Šime Vidas
  • 182,163
  • 62
  • 281
  • 385

1 Answers1

4

Thanks to Nico for pointing out that JavaScript RegExp has (?!) functionality. The solution seems to be:

/^\/(?!admin|db)/

enter image description here

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
  • Do you perhaps want to include the `/` inside the lookahead? Like, `/^\/(?!(?:admin|db)\/)/`? That way you'd allow "/administrator/" (which may or may not be important to you). – Pointy Aug 30 '14 at 15:14
  • @Pointy Yes, this is a slight improvement :) (With this, I will also catch "/admin" or "/db" but those paths don't make sense anyway.) – Šime Vidas Aug 30 '14 at 15:23