From bca820ff5ebe4fba966fb91d6984d132aef3d9cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20=20Sk=C3=B6ld?= Date: Fri, 28 Jun 2024 23:36:03 +0200 Subject: [PATCH] Improve matching clarity --- web/js/textbox.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/web/js/textbox.js b/web/js/textbox.js index a8f5f1e..102f279 100644 --- a/web/js/textbox.js +++ b/web/js/textbox.js @@ -115,22 +115,20 @@ app.registerExtension({ let highlightedText = ''; let lastIndex = 0; - const regex = /(?:[^\\]|^)(\()|(?:[^\\]|^)(\))/g; + const regex = /\\.|(\()|(\))/g; let match; while ((match = regex.exec(text)) !== null) { - // Calculate the correct match index considering the non-capturing group - let matchIndex = match.index + match[0].length - 1; - - if (match[1]) { // if it matches '(' + if (match[0][0] === '\\') { + continue; + } else if (match[1]) { // if it matches '(' const color = colors[nestingLevel % colors.length]; + highlightedText += text.slice(lastIndex, match.index) + `${match[1]}`; nestingLevel++; - highlightedText += text.slice(lastIndex, matchIndex) + `${match[1]}`; } else if (match[2]) { // if it matches ')' nestingLevel--; - const color = colors[nestingLevel % colors.length]; - highlightedText += text.slice(lastIndex, matchIndex) + `${match[2]}`; + highlightedText += text.slice(lastIndex, match.index) + `${match[2]}`; } - lastIndex = matchIndex + 1; + lastIndex = regex.lastIndex; } highlightedText += text.slice(lastIndex);