Finalize Webview Regex Fixes: Correctly output regex literals without breaking JS parsing using 1-level escaping inside TS template string.

This commit is contained in:
Jay
2026-04-14 12:16:03 +09:00
parent 058c52d11a
commit 39f6eb62d5
2 changed files with 321 additions and 92 deletions
+2 -1
View File
@@ -1 +1,2 @@
`h.replace(/(\/\/[^\n]*)/g)`
var code = "\nfunction highlight(code,lang){\n let h=esc(code);\n h=h.replace(/(\\/\\/[^\\n]*)/g,'<span class=\"cm\">$1</span>');\n h=h.replace(/(#[^\\n]*)/g,'<span class=\"cm\">$1</span>');\n h=h.replace(/(\\/\\*[\\s\\S]*?\\*\\/)/g,'<span class=\"cm\">$1</span>');\n h=h.replace(/(&quot;[^&]*?&quot;|&#x27;[^&]*?&#x27;)/g,'<span class=\"str\">$1</span>');\n h=h.replace(/\\b(function|const|let|var|return|if|else|for|while|class|import|export|from|default|async|await|try|catch|throw|new|this|def|self|print|lambda|yield|with|as|raise|except|finally)\\b/g,'<span class=\"kw\">$1</span>');\n h=h.replace(/\\b(\\d+\\.?\\d*)\\b/g,'<span class=\"num\">$1</span>');\n h=h.replace(/\\b(True|False|None|true|false|null|undefined|NaN)\\b/g,'<span class=\"num\">$1</span>');\n h=h.replace(/\\b(String|Number|Boolean|Array|Object|Map|Set|Promise|void|int|float|str|list|dict|tuple)\\b/g,'<span class=\"type\">$1</span>');\n h=h.replace(/([=!&lt;&gt;+\\-*/%|&amp;^~?:]+)/g,'<span class=\"op\">$1</span>');\n}\n";
console.log(code);