Skip to content

Instantly share code, notes, and snippets.

@thomaswilburn
Last active April 27, 2016 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaswilburn/308685708f8f734fa5f74a71fa03882f to your computer and use it in GitHub Desktop.
Save thomaswilburn/308685708f8f734fa5f74a71fa03882f to your computer and use it in GitHub Desktop.
A template literal tag that lets you write verbose regex syntax
var r = require("./verboseRegex.js");
var regex = r`
^ # from the start of the string
\s* # with any number of spaces
\(\s+ # find the first paren, followed by whitespace
([^)]+) # capture anything that's not a closing paren
\s*\) # followed by whitespace and a closing paren
`;
console.log(r); // logs out /^\s*\(\s+([^)]+)\s*\)/;
var regex = function(parts) {
var stripped = parts.raw.join("").replace(/\s|\n|\s*#.*$/gm, "");
return new RegExp(stripped);
};
module.exports = regex;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment