regexp-make-js icon indicating copy to clipboard operation
regexp-make-js copied to clipboard

How are flags of interpolated regexes treated?

Open bergus opened this issue 10 years ago • 3 comments
trafficstars

The usage section says

RegExp instances 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?

bergus avatar Aug 12 '15 03:08 bergus

Ah, I've found #flags:

When a case-insensitive RegExp is interpolated into a case-sensitive one, the interpolated one still matches case insensitively.

Also, there's an example where RegExp.make ${//i}[a-z0-9_]*${/</foo>/}`` evaluates to /(?:<[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?

bergus avatar Aug 12 '15 04:08 bergus

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 [^...].

mikesamuel avatar Aug 12 '15 06:08 mikesamuel

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.

bergus avatar Aug 12 '15 07:08 bergus