0

Im working on a node.js app, and im doing router matching.

I need to match all routes with all variables except the ones which begin with "public , static , files or same words with added "/"

i know i could do it using an if statement before regexp, to check if those words are withing url, and if they are, skip regexp, but i dont want to add such nesting, and knowing how to do it using regexp will become in handy in the future anyways.

i know how to match anything except...some letters, ie ^[0-9] , but i cant use the same for words. I googled and found that lookahead could solve this, but... i cant get it to work.

In the end, id like to use something like this (in pseudo code) where the .+ would match only if the pattern does not match any of the given words. match(/^(?!public|static|files) .+ /gi)

edit 1:

The format of the url's would be something like this..with or without slashes.

/controller/action/4/var:something/

i want to make a regexp that matches this controller - action - id pattern, but at the same time wouldnt match patterns like this /public/images/4 or static/files/somefile

in general, id like to know how to match a pattern, but only if it doesnt begin with given words.

e.g something like this...but it doesnt work ( match .+, but only if it doesnt contain the words mentioned before /^(?!public|static|files).+ /gi)

Rainer Plumer
  • 3,693
  • 2
  • 24
  • 42
  • i posted the examples that i've tried, and googled. googling it mostly brings up simple ^ which works in [], but not with words – Rainer Plumer Nov 06 '13 at 19:07
  • Can you give an example of the format of the input file and what exactly you want matched? (If you haven't solved this already) – quazzieclodo Nov 08 '13 at 22:12
  • updated my post, edit 1 – Rainer Plumer Nov 08 '13 at 23:04
  • 1
    How about `if(!/^(public|static|files)/i.test(url)) { ... }`? – Flambino Nov 08 '13 at 23:31
  • This is pretty much what i ended up using, an if statement to check if the regex matches any of the words i dont want, and if not, make another test to get the matching groups. But the ideal option would have been one regex, which doesnt match if those words are present, and if not, would match any patterns that followed – Rainer Plumer Nov 08 '13 at 23:39

1 Answers1

0

Actually, I'm not having trouble with negative look-aheads. Something like this seems to work just fine, although it's not super extensible.

/^\/(?!public|static|files)([^\/]+)?\/?([^\/]+)?\/?([^\/]+)?\/?(.*)$/i

1st capture will be the controller, 2nd is the action, 3rd is the ID, and 4th is whatever is left.

See this jsfiddle

Flambino
  • 18,507
  • 2
  • 39
  • 58
  • This will work fine, but it will ignore url's starting with ./controller/action or controller/action, but thats not a problem. – Rainer Plumer Nov 09 '13 at 16:02
  • @RainerPlumer Yeah, you'll likely have to do some sort of normalization before matching (or maybe the regex can be more flexible, but it's already complicated enough). Normalization will be necessary sooner or later, though, regardless of how you do the matching – Flambino Nov 09 '13 at 16:06