Closed taralx closed 3 years ago
Best I have for you is this mess:
RegExp(`^${
// eslint-disable-next-line
(base + route).replaceAll(/([/.])?(?::(\w+)(\?)?|(\*)(\.)?)|([[{()\\.?+^$|])|\/$/g, (m,p='',v,q,s,d='',c) => c?'\\'+c:(d?'':'(')+(p&&'\\'+p)+(d&&'(')+(v?`(?<${v}>[^/]+?)`:s?'.*'+(d&&'\\.'):'')+')'+(q||s?'?':''))
}/*$`),
Broken down regex:
/
([/.])? # (p)refix: included in the optionality
(?:
:(\w+) # (v)ariable
(\?)? # (q)uestion mark
|
(\*) # (s)tar
(\.)? # (d)ot that excludes preceding / or . if present
)
|
([[{()\\.?*+^$|]) # regexp characters that need quoting
|
\/$ # trim trailing slash
/g
Ooof... to be fair, I don't think simple dots were ever escaped (oversight), but hopefully we can pull this fix off with minimal characters. It's already killing me that we're doing 4 regex passes in the current version (so many characters!) ðŸ˜
Yeah, the solution I posted is like 50 bytes. :(
I have this working and did some refactoring of the tests to more easily allow bulk route tests...
I'm only addressing escaping dots, as that's a pretty common use-case that I overlooked... not escaping all possible regex collisions. I'm figuring out which ones work/don't work as I go!
Example
'/static/file.json'
results in/static/file.json
, with the extension dot unescaped. This is somewhat related to https://github.com/kwhitley/itty-router/issues/34.