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
  • 68,531
  • 11
  • 88
  • 149
Šime Vidas
  • 163,768
  • 59
  • 261
  • 366

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
  • 163,768
  • 59
  • 261
  • 366
  • 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