path-to-regexp icon indicating copy to clipboard operation
path-to-regexp copied to clipboard

Path matching greater than > and lesser than < symbols

Open LokeshNSF opened this issue 4 years ago • 7 comments

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.

LokeshNSF avatar Nov 30 '21 17:11 LokeshNSF

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

blakeembrey avatar Nov 30 '21 18:11 blakeembrey

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

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

LokeshNSF avatar Nov 30 '21 18:11 LokeshNSF

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

blakeembrey avatar Dec 01 '21 05:12 blakeembrey

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 avatar Dec 01 '21 06:12 LokeshNSF

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

LokeshNSF avatar Dec 01 '21 06:12 LokeshNSF

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

blakeembrey avatar Dec 03 '21 20:12 blakeembrey

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

LokeshNSF avatar Dec 04 '21 06:12 LokeshNSF