From 5c730e8b95e484720886a6183f3998e7bdfb47e6 Mon Sep 17 00:00:00 2001 From: Antonino Porcino Date: Tue, 2 Aug 2016 17:14:42 +0200 Subject: [PATCH] updated comment explaining the regex --- src/reactTemplates.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/reactTemplates.js b/src/reactTemplates.js index 053d8e0..c6e2f04 100644 --- a/src/reactTemplates.js +++ b/src/reactTemplates.js @@ -466,11 +466,20 @@ function convertHtmlToReact(node, context) { * @throws {String} the part of the string that failed to parse */ function parseScopeSyntax(text) { - // in plain english, this regex scans for: - // any character + one or more space + "as" + one or more space + JavaScript identifier + - // zero or more space + semicolon or end of line + zero or more space - // it captures "any character" as the scope expression, "JavaScript identifier" as the identifier - const regex = RegExp('([\\s\\S]*?)(?: )+as(?: )+([$_a-zA-Z]+[$_a-zA-Z0-9]*)(?: )*(?:;|$)(?: )*', 'g'); + // the regex below was built using the following pseudo-code: + // double_quoted_string = `"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"` + // single_quoted_string = `'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'` + // text_out_of_quotes = `[^"']*?` + // 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 = []; do { const idx = regex.lastIndex;