While looking in to proposals for look-behinds and named capture groups for
ECMAScript RegExps, I thought I'd also think about other oft-requested features
we currently lack: free-spacing and comments. Comments allow a programmer to
embed comments inside the regexp literal. Free-spacing tells the RegExp engine
to ignore spaces, tabs, and line breaks. These two features go really nicely
together to allow human readable regexps - different parts of a pattern can be
split into separate lines with comments on each line describing what it
matches. XRegExp supports both of these features as well as the RegExp engines
in Perl, Java, C# and others.
One challenge with supporting free-spacing in ECMAScript is that we don't allow
line breaks inside our regexp literal and constructing regexps from strings is
somewhat annoying. The best we could have right now (I think) is something like:
```
let re = new RegExp(String.raw`
(\d{3}-)? # area code (optional)
\d{3}- # prefix
\d{4} # line number
`, "x");
```
I think this is still a win for long confusing patterns, but maybe I'm alone!
Is free-spacing and comments still reasonable if we have to use string
templates? Or is there a nice way to extend the grammar of regular expression
literals to allow for line breaks? (Eg. maybe we only allow free-spacing with a
mode specifier like (?x) inside the pattern?) Any other thoughts?
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss