RegExp free-spacing & comments
C. Scott Ananian
ecmascript at cscott.net
Fri Nov 6 18:05:00 UTC 2015
If you're using string templates, why not do the full regex there,
instead of just passing the result to `new RegExp`?
See https://esdiscuss.org/topic/regexp-escape#content-22 and
https://esdiscuss.org/topic/regexp-escape#content-28 for some
examples, and https://github.com/benjamingr/RegExp.escape/issues/37#issuecomment-126785041
for a discussion of creating a new template string function called
`re`.
--scott
On Fri, Nov 6, 2015 at 12:28 PM, Brian Terlson
<Brian.Terlson at microsoft.com> wrote:
> 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
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
More information about the es-discuss
mailing list