Last active
February 13, 2022 22:50
-
-
Save westc/dc1b74018d278147e05cac3018acd8e5 to your computer and use it in GitHub Desktop.
A tag function which makes it possible to create verbose regular expressions in JavaScript. A verbose regular expression is one which ignores the whitespace unless it is escaped with a leading backslash.
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
/** | |
* A tag function which can be used to create verbose regular expressions. | |
* @license Copyright 2021 - Chris West - MIT Licensed | |
* @see https://gist.github.com/westc/dc1b74018d278147e05cac3018acd8e5 | |
*/ | |
function vRegExp(input, ...fillins) { | |
let {raw} = input; | |
let content = raw[0]; | |
for (let i = 1, l = raw.length; i < l; i++) { | |
content += fillins[i - 1] + raw[i]; | |
} | |
content = content | |
// Escapes whitespaces within character classes. | |
.replace( | |
/(^|[^\\])\[([^\\]+|\\.)\]/g, | |
(_, m1, mX) => (m1 + '[' + mX.replace(/\s/g, '\\$&') + ']') | |
) | |
// Removes whitespaces and JS-style comments. | |
.replace(/(\\[^])|\s+|\/\/.*|\/\*[^]*?\*\//g, '$1'); | |
return new RegExp( | |
// Removes the leading flags (eg. "(?i)"). | |
content.replace(/^(?:\s*\(\?\w+\))+/g, ''), | |
// Only keeps the leading flags (eg. "(?i)" becomes "i"). | |
content.replace(/\(\?(\w+)\)|[^(]+|\(/g, '$1') | |
); | |
} |
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
// HIDE \\ | |
console.load('local://vRegExp.js'); | |
// \\ | |
// Regular expression used by srt2vtt to parse subtitles. | |
var lineBreak = String.raw`(?:\r?\n|\r)`; | |
var RGX_SRT = vRegExp` | |
(?gm) // Global and multiline | |
^\d+$ // Subtitle number indicator | |
${lineBreak} // Line break | |
^(?<time1a>[\d:]+)[\.,](?<time1b>\d+) // Line starting with time range | |
\s*-->\s* // Time range separator | |
(?<time2a>[\d:]+)[\.,](?<time2b>\d+)$ // Line ending with time range | |
${lineBreak} // Line break | |
(?<text>(?:^[^\r\n]+$\r?\n)+) // One or more lines of text | |
${lineBreak} // Line break | |
`; | |
console.log(RGX_SRT); | |
// \\ | |
var srtCode = ` | |
1 | |
00:00:00,000 --> 00:00:01,053 | |
Hello crazy | |
world! | |
1 | |
00:00:01,054 --> 00:00:02,987 | |
This is a good test. | |
`; | |
console.log(RGX_SRT.exec(srtCode)); |
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 runIOApp(strRegExp) { | |
return vRegExp`${strRegExp}`.toString(); | |
} |
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
{ | |
// Where the JS code lies | |
"files": ["vRegExp.js", "~io-app.js"], | |
// Name of the function responsable for transforming the input into the output. | |
"transform": "runIOApp", | |
// Input setup | |
"input": { | |
"language": "text", | |
"label": "Input - Verbose Regular Expression", | |
"value": "(?i) // Ignore casing just because we can ;)\n^([a-z0-9_\\.-]+) // local Part\n@ // single @ sign\n([0-9a-z\\.-]+) // Domain name\n\\. // single Dot\n([a-z]{2,6})$ // Top level Domain" | |
}, | |
// Output setup | |
"output": { "language": "javascript", "label": "Output - JavaScript Regular Expression" }, | |
// Type of YourJS IO-App | |
"type": "split", | |
// title | |
"title": "Verbose Regular Expressions in JavaScript", | |
// description | |
"description": "Use the custom vRegExp tag function which turns a template string into a regular expression while simulating a verbose regular expression." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run the example file:
https://www.yourjs.com/console/?gist=dc1b74018d278147e05cac3018acd8e5&file=~example.js&skipIntro=1