Last active
August 31, 2022 07:43
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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