regexp-make-js
regexp-make-js copied to clipboard
How are flags of interpolated regexes treated?
The usage section says
RegExpinstances are treated like the set of substrings they match
Does that apply to flags as well? So that
RegExp.make `a${ /b/i }`
evaluates to
/a(?i:b)/
(modifier span, not yet a JS RegExp feature) or an equivalent of that?
Ah, I've found #flags:
When a case-insensitive
RegExpis interpolated into a case-sensitive one, the interpolated one still matches case insensitively.
Also, there's an example where RegExp.make ${//(?:<[Ff][Oo][Oo]>)[a-z0-9_]*(?:<\/foo>)/. But is that really feasible and works in all cases? What about other flags than case-insensitivity, I'd expect /m and /u to be quite non-trivial?
RegExp.make('i')`/foo/`
specifies the i flag for the output.
The handling of ^ with the m flag is complicated by the lack of lookbehind. I think the other cases are feasible if fiddly to get right.
The handling for supplemental code-points with the 'u' flag might be tricky, especially when the embedding and embedded contexts disagree about the size of a code-unit matched by [^...].
Yup, fiddly for sure :-) Let's be optimistic that this is possible for all flags, but we need to keep in mind that it could be potentially problematic. Introducing modifiers for groups as a new syntax feature would greatly simplify this, but is equally hard to polyfill.