From ebb1b79b73a02fd70af3160993c34702b7e97846 Mon Sep 17 00:00:00 2001 From: fofolee Date: Sun, 25 Feb 2024 12:55:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8JSON.stringify=E8=BD=AC?= =?UTF-8?q?=E4=B9=89=E7=89=B9=E6=AE=8A=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/CommandRunResult.vue | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/components/CommandRunResult.vue b/src/components/CommandRunResult.vue index a6b45d0..44315b9 100644 --- a/src/components/CommandRunResult.vue +++ b/src/components/CommandRunResult.vue @@ -156,14 +156,13 @@ export default { }; document.addEventListener("keydown", this.ctrlCListener); }, - // 转义特殊变量里的特殊字符和换行符 escapeChars(string) { - return string - // 转义特殊字符 - .replace(/`|'|"|\\/g, "\\$&") - // 转义换行字符 - .replace(/\r\n/g, "\\r\\n") - .replace(/\n/g, "\\n") + // 通过JSON.stringify,将特殊变量里所有特殊字符转义,输出为一个带双引号的字符串 + return JSON.stringify(string) + // 去掉两边双引号 + .slice(1, -1) + // 单独转义单引号和反引号 + .replace(/`|'/g, "\\$&") }, // 特殊变量赋值 assignSpecialVars(cmd) {