如何删除从 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
How to remove backslash escaping from a javascript var?
提问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#replace
function:
您可以通过正则表达式和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 g
at the end of the regex tells replace
to work throughout the string ("global"); otherwise, it would replace only the first match.
将g
在正则表达式的结尾告诉replace
工作在整个字符串(“全球”); 否则,它只会替换第一个匹配项。
回答by Tony Brix
You can use JSON.parse
to unescape slashes:
您可以使用JSON.parse
unescape 斜杠:
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
/(.)
是一个捕获组,用于捕获反斜杠后面的内容
/g
global matching: Find all matches, not just the first.
/g
全局匹配:查找所有匹配项,而不仅仅是第一个。
$1
is 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.
此功能不会简单地删除斜线。文本编译为代码,如果输入正确,您将获得任何转义序列的正确未转义结果。