Last active
June 19, 2022 05:36
-
-
Save keleshev/c49465caed1f114b2bb3f2b730e221ca to your computer and use it in GitHub Desktop.
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
function verboseRegExp(input) { | |
if (input.raw.length !== 1) { | |
throw Error('verboseRegExp: interpolation is not supported'); | |
} | |
let source = input.raw[0]; | |
let regexp = /(?<!\\)\s|[/][/].*|[/][*][\s\S]*[*][/]/g; | |
let result = source.replace(regexp, ''); | |
return new RegExp(result); | |
} | |
let example1 = new RegExp(verboseRegExp` | |
(?<!\\) \s // Ignore whitespace, but not | |
// when escaped with backslash. | |
| [/][/] .* // Single-line comment. | |
| [/][*] [\s\S]* [*][/] // Multi-line comment. | |
// Note: /[\s\S]/ is same as /./s | |
// with dot-all flag (s). | |
`, 'g'); | |
console.log(example1); | |
let example2 = verboseRegExp` | |
(0 | [1-9] [0-9]*) // Integer part with no leading zeroes | |
\. // Dot | |
[0-9]* // Fractional part (optional) | |
([eE] [+-]? [0-9]+)? // Exponent part (optional) | |
`; | |
console.log(example2); | |
function assertRegExpEqual(regexp1, regexp2) { | |
if (regexp1.toString() !== regexp2.toString()) { | |
console.error(`Assertion error: ${regexp1} !== ${regexp2}`); | |
} else { | |
console.log('.'); | |
} | |
} | |
assertRegExpEqual(verboseRegExp`hai`, /hai/); | |
assertRegExpEqual(verboseRegExp`[]`, /[]/); | |
// Comments and whitespace are ignored; | |
assertRegExpEqual(verboseRegExp`x y`, /xy/); | |
assertRegExpEqual(/xy/, verboseRegExp` | |
x // ignored | |
y | |
`); | |
assertRegExpEqual(verboseRegExp`x /* ignored */ y`, /xy/); | |
// Also inside character classes: | |
assertRegExpEqual(verboseRegExp`[x y]`, /[xy]/); | |
assertRegExpEqual(verboseRegExp`[x//comment | |
y]`, /[xy]/); // XXX | |
assertRegExpEqual(verboseRegExp`[x/*z][x*/z]`, /[xz]/); | |
assertRegExpEqual(verboseRegExp`[x/*z*/y]`, /[xy]/); | |
// Escaped whitespace is not ignored: | |
assertRegExpEqual(verboseRegExp`\ `, /\ /); | |
assertRegExpEqual(verboseRegExp`\ x `, /\ x/); | |
assertRegExpEqual(verboseRegExp`\ `, /\ /); | |
// Also not in brackets: | |
assertRegExpEqual(verboseRegExp`[\ ]`, /[\ ]/); | |
// Other escapes are passed through | |
assertRegExpEqual(verboseRegExp`\s`, /\s/); | |
assertRegExpEqual(verboseRegExp`\n`, /\n/); | |
// Nested brackets are allowed | |
assertRegExpEqual(verboseRegExp` [x [y ]] `, /[x[y]]/); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related work: http://xregexp.com