What’s wrong with this? ES2015 introduced the /u flag, which made certain previously-invalid patterns valid, and vice versa. The same pattern can be valid when the /u flag is set, and invalid without /u. https://mathiasbynens.be/notes/es6-unicode-regex … RegExp pattern validity depends on the flags!
-
-
Show this thread
-
No problem, I hear you say. Just change the validator to consider flags as well: function isValidRegExp(pattern, flags) { try { new RegExp(pattern, flags); return true; } catch { return false; } }
Show this thread -
But this is still problematic whenever new flags are added to the language — the validator now rejects spec-compliant RegExps in some environments, but not in others, depending on which flags they support.
Show this thread -
This happened with the recent /s flag (https://mathiasbynens.be/notes/es-regexp-proposals#dotAll …), and back in 2015 with the /u flag.
Show this thread -
Here’s the horrible hack I landed in Esprima to support the /u flag (even in environments that didn’t yet support it): https://github.com/jquery/esprima/blob/c0dfa42da4da9fa242dac623278a082cb38edc20/src/scanner.ts#L1089-L1141 … It made its way into other parsers.
Show this thread -
It had known limitations, but it’s the best you can do without pulling in a full JS RegExp parser or validator, which seemed like overkill.
Show this thread -
https://github.com/shapesecurity/shift-regexp-acceptor-js … makes it look like it doesn’t have to be overkill. It’s an extremely lightweight JS RegExp acceptor that implements the full spec grammar.
Show this thread
End of conversation
New conversation -
-
-
And because it was 2014, they had to have an unused catch parameter!
-
The pattern, not the exact code :P (I seriously pondered whether or not to add it to the tweet or not but decided against it)
End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.
JavaScript, HTML, CSS, HTTP, performance, security, Bash, Unicode, i18n, macOS.