Last active
October 26, 2017 16:14
-
-
Save LukeChannings/706228926544d9fc622418738ae9c470 to your computer and use it in GitHub Desktop.
Tagged regular expressions
This file contains hidden or 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
const tre = (strings, ...values) => { | |
const flags = values | |
.filter(v => v instanceof RegExp) | |
.map(re => re.flags) | |
.reduce( | |
(a, b) => | |
a + | |
b | |
.split("") | |
.filter(char => !a.includes(char)) | |
.join("") | |
) | |
return new RegExp( | |
String.raw( | |
strings, | |
...values.map(v => (v instanceof RegExp ? v.source : v)) | |
), | |
flags | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
In the example,
re
will equal/((?:\s+)?|[A-Za-z0-9])?[^\s\u0022\u0027\u003e\u0025\u003d\u0000-\u001f\u007f-\u009f]+/iu
It is also possible to use comments, like:
Or