如何删除从 javascript var 转义的反斜杠?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6640382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:35:45  来源:igfitidea点击:

How to remove backslash escaping from a javascript var?

javascriptescaping

提问by BrunoLM

I have this var

我有这个 var

var x = "<div class=\\"abcdef\\">";

Which is

这是

<div class=\"abcdef\">

But I need

但是我需要

<div class="abcdef">

How can I "unescape" this var to remove all escaping characters?

如何“取消转义”此 var 以删除所有转义字符?

回答by T.J. Crowder

You can replace a backslash followed by a quote with just a quote via a regular expression and the String#replacefunction:

您可以通过正则表达式和String#replace函数将反斜杠后跟引号替换为引号:

var x = "<div class=\\"abcdef\\">";
x = x.replace(/\"/g, '"');
document.body.appendChild(
  document.createTextNode("After: " + x)
);

Note that the regex just looks for one backslash; there are two in the literal because you have to escape backslashes in regular expression literals with a backslash (just like in a string literal).

请注意,正则表达式只查找一个反斜杠;文字中有两个,因为您必须使用反斜杠转义正则表达式文字中的反斜杠(就像在字符串文字中一样)。

The gat the end of the regex tells replaceto work throughout the string ("global"); otherwise, it would replace only the first match.

g在正则表达式的结尾告诉replace工作在整个字符串(“全球”); 否则,它只会替换第一个匹配项。

回答by Tony Brix

You can use JSON.parseto unescape slashes:

您可以使用JSON.parseunescape 斜杠:

function unescapeSlashes(str) {
  // add another escaped slash if the string ends with an odd
  // number of escaped slashes which will crash JSON.parse
  let parsedStr = str.replace(/(^|[^\])(\\)*\$/, "$&\");

  try {
    parsedStr = JSON.parse(`"${parsedStr}"`);
  } catch(e) {
    return str;
  }
  return parsedStr ;
}

回答by Brixomatic

If you want to remove backslash escapes, but keep escaped backslashes, here's what you can do:

如果您想删除反斜杠转义,但保留转义的反斜杠,您可以执行以下操作:

"a\b\\c\\\\\d".replace(/(?:\(.))/g, '');

Results in: ab\c\\d.

结果: ab\c\\d

Explanation of replace(/(?:\\(.))/g, '$1'):

的解释replace(/(?:\\(.))/g, '$1')

/(?:\\)is a non-capturing group to capture the leading backslash

/(?:\\)是一个非捕获组来捕获前导反斜杠

/(.)is a capturing group to capture what's following the backslash

/(.)是一个捕获组,用于捕获反斜杠后面的内容

/gglobal matching: Find all matches, not just the first.

/g全局匹配:查找所有匹配项,而不仅仅是第一个。

$1is referencing the content of the first capturing group (what's following the backslash).

$1正在引用第一个捕获组的内容(反斜杠后面的内容)。

回答by ElonU Webdev

Try this:

尝试这个:

x = x.replace(/\/g, "");

回答by Alex Emilov

var x = "<div class=\\"abcdef\\">";
alert(x.replace(/\/gi, ''));

回答by Aryeh Armon

'<div class=\\"abcdef\\">'.replace(/\\"/g, '"')

Other answers have you delete all backslashes, you only want to delete the ones befoew the quotes.

其他答案让您删除所有反斜杠,您只想删除引号之前的那些。

回答by user2921779

You need to make there be one backslash instead of three.
Like this:

您需要设置一个反斜杠而不是三个。
像这样:

var x = "<div class=\"abcdef\">";        

回答by atticus

Let me propose this variant:

让我提出这个变体:

function un(v) { eval('v = "'+v+'"'); return v; }

This function will not simply remove slashes. Text compiles as code, and in case correct input, you get right unescaping result for any escape sequence.

此功能不会简单地删除斜线。文本编译为代码,如果输入正确,您将获得任何转义序列的正确未转义结果。