pillarjs / path-to-regexp

Turn a path string such as `/user/:name` into a regular expression
MIT License
8.12k stars 381 forks source link

Path matching greater than > and lesser than < symbols #263

Closed LokeshNSF closed 2 months ago

LokeshNSF commented 2 years ago

I am trying to match greater than > and lesser than < symbols in the path of Express 4.17.1, but it is not working. I tried regex match using /[>\<]/, but it is not matching a path with /test/.

Is there any special handling that we need to do to handle this like how it is for $ where we use ([$])? Any help here is appreciated.

blakeembrey commented 2 years ago

A regex is just a regex, but why would [<>] match /test/?

LokeshNSF commented 2 years ago

A regex is just a regex, but why would [<>] match /test/?

Sorry that was a typo, it was not matching a path with /test/

blakeembrey commented 2 years ago

That's correct though. There are no characters of >< in /test/.

LokeshNSF commented 2 years ago

That's correct though. There are no characters of >< in /test/.

I think whatever I am writing is not getting shown (I have added in quotes now sorry :() what I meant was Sorry that was a typo, it was not matching a path with /test/<param>

LokeshNSF commented 2 years ago

I tried couple of other things too, /(><)/ , /.(><)./, and /.[><]./ but none of these regex are matching this path /test/<param>

blakeembrey commented 2 years ago

There's probably two issues you're having:

  1. /[><]/ matches because it's a regex, the others you wrote don't make sense. You can test a regex like usual, e.g. /[><]/.exec('/test/<param>').
  2. If you're trying to match this coming from a URL path, < and > are not valid characters and would be URL escaped so matching on < and > is impossible to do, you'd want the URL encoded versions instead
LokeshNSF commented 2 years ago

Got it, Thanks @blakeembrey. /(%3E|%3C)/ works.