Mark S. Miller (2015-06-13T06:38:57.000Z)
The point of this last variant is that data gets escaped but RegExp objects
do not -- allowing you to compose RegExps:   re`${re1}|${re2}*|${data}`
But this requires one more adjustment:


>
>   function re(first, ...args) {
>     let flags = first;
>     function tag(template, ...subs) {
>       const parts = [];
>       const numSubs = subs.length;
>       for (let i = 0; i < numSubs; i++) {
>         parts.push(template.raw[i]);
>         const subst = subs[i] instanceof RegExp ?

               `(?:${subs[i].source})` :

>             subs[i].replace(/[\/\\^$*+?.()|[\]{}]/g, '\\amp;');
>         parts.push(subst);
>       }
>       parts.push(template.raw[numSubs]);
>       return RegExp(parts.join(''), flags);
>     }
>     if (typeof first === 'string') {
>       return tag;
>     } else {
>       flags = void 0;  // Should this be '' ?
>       return tag(first, ...args);
>     }
>   }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150612/8df7931d/attachment.html>
d at domenic.me (2015-06-16T16:54:20.699Z)
The point of this last variant is that data gets escaped but RegExp objects
do not -- allowing you to compose RegExps:   re`${re1}|${re2}*|${data}`
But this requires one more adjustment:


```
>
>   function re(first, ...args) {
>     let flags = first;
>     function tag(template, ...subs) {
>       const parts = [];
>       const numSubs = subs.length;
>       for (let i = 0; i < numSubs; i++) {
>         parts.push(template.raw[i]);
>         const subst = subs[i] instanceof RegExp ?

               `(?:${subs[i].source})` :

>             subs[i].replace(/[\/\\^$*+?.()|[\]{}]/g, '\\amp;');
>         parts.push(subst);
>       }
>       parts.push(template.raw[numSubs]);
>       return RegExp(parts.join(''), flags);
>     }
>     if (typeof first === 'string') {
>       return tag;
>     } else {
>       flags = void 0;  // Should this be '' ?
>       return tag(first, ...args);
>     }
>   }
```