Skip to content

Instantly share code, notes, and snippets.

@KooiInc
Last active August 31, 2022 07:43
Show Gist options
  • Save KooiInc/e996c103fb27739fd365cc2817fb9308 to your computer and use it in GitHub Desktop.
Save KooiInc/e996c103fb27739fd365cc2817fb9308 to your computer and use it in GitHub Desktop.
ES - tagged template function to create a regular expression with comments and whitespace
export default (regexStr, ...args) => {
const flags = Array.isArray(args.slice(-1)) ? args.pop().join('') : ``;
return new RegExp(
(args.length &&
regexStr.raw.reduce( (a, v, i ) => a.concat(args[i-1] || ``).concat(v), ``) ||
regexStr.raw.join(``))
.split(`\n`)
.map( line => line.replace(/\s|\/\/.*$/g, ``).trim().replace(/@s/g, ` `) )
.join(``), flags );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment