Last active
April 27, 2016 23:30
-
-
Save thomaswilburn/308685708f8f734fa5f74a71fa03882f to your computer and use it in GitHub Desktop.
A template literal tag that lets you write verbose regex syntax
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
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*\)/; |
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
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