1
0
mirror of https://github.com/bobwen-dev/react-templates synced 2025-04-12 00:56:39 +02:00

updated comment explaining the regex

This commit is contained in:
Antonino Porcino 2016-08-02 17:14:42 +02:00 committed by ido
parent f3fc45f119
commit 5c730e8b95

View File

@ -466,11 +466,20 @@ function convertHtmlToReact(node, context) {
* @throws {String} the part of the string that failed to parse * @throws {String} the part of the string that failed to parse
*/ */
function parseScopeSyntax(text) { function parseScopeSyntax(text) {
// in plain english, this regex scans for: // the regex below was built using the following pseudo-code:
// any character + one or more space + "as" + one or more space + JavaScript identifier + // double_quoted_string = `"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"`
// zero or more space + semicolon or end of line + zero or more space // single_quoted_string = `'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'`
// it captures "any character" as the scope expression, "JavaScript identifier" as the identifier // text_out_of_quotes = `[^"']*?`
const regex = RegExp('([\\s\\S]*?)(?: )+as(?: )+([$_a-zA-Z]+[$_a-zA-Z0-9]*)(?: )*(?:;|$)(?: )*', 'g'); // expr_parts = double_quoted_string + "|" + single_quoted_string + "|" + text_out_of_quotes
// expression = zeroOrMore(nonCapture(expr_parts)) + "?"
// id = "[$_a-zA-Z]+[$_a-zA-Z0-9]*"
// as = " as" + OneOrMore(" ")
// optional_spaces = zeroOrMore(" ")
// semicolon = nonCapture(or(text(";"), "$"))
//
// regex = capture(expression) + as + capture(id) + optional_spaces + semicolon + optional_spaces
const regex = RegExp("((?:(?:\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"|'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'|[^\"']*?))*?) as(?: )+([$_a-zA-Z]+[$_a-zA-Z0-9]*)(?: )*(?:;|$)(?: )*", 'g');
const res = []; const res = [];
do { do {
const idx = regex.lastIndex; const idx = regex.lastIndex;