On Sat, Jun 13, 2015 at 1:51 AM, Mark S. Miller <[email protected]> wrote:
> Nice! Inspired > > // Based on > // https://github.com/benjamingr/RexExp.escape/blob/master/polyfill.js > function re(template, ...subs) { > const parts = []; > const numSubs = subs.length; > for (let i = 0; i < numSubs; i++) { > parts.push(template.raw[i]); > parts.push(subs[i].replace(/[\/\\^$*+?.()|[\]{}]/g, '\\$&')); > } > parts.push(template.raw[numSubs]); > return RegExp(parts.join('')); > } > A slight tweak allows you to pass flags: ``` function re(flags, ...args) { if (typeof template !== 'string') { // no flags given return re(undefined)(flags, ...args); } return function(template, ...subs) { const parts = []; const numSubs = subs.length; for (let i = 0; i < numSubs; i++) { parts.push(template.raw[i]); parts.push(subs[i].replace(/[\/\\^$*+?.()|[\]{}]/g, '\\$&')); } parts.push(template.raw[numSubs]); return RegExp(parts.join(''), flags); }; } ``` Use like this: ``` var r = re('i')`cAsEiNsEnSiTiVe`; ``` --scott
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

