如何在 JavaScript 中转义和取消转义引号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21451650/
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-10-27 20:56:26  来源:igfitidea点击:

How to escape and unescape quotes in JavaScript?

javascriptregexstringreplace

提问by Emil A.

Here's a short piece of code:

这是一段简短的代码:

var utility = {
    escapeQuotes: function(string) {
        return string.replace(new RegExp('"', 'g'),'\"');
    },
    unescapeQuotes: function(string) {
        return string.replace(new RegExp('\"', 'g'),'"');
    }
};

var a = 'hi "';

var b = utility.escapeQuotes(a);
var c = utility.unescapeQuotes(b);

console.log(b + ' | ' + c);

I would expect this code to work, however as a result I receive:

我希望此代码能够正常工作,但结果我收到:

hi \" | hi \"

If I change the first parameter of the new RegExp constructor in the unescapeQuotes method to 4 backslashes everything starts working as it should.

如果我将 unescapeQuotes 方法中新 RegExp 构造函数的第一个参数更改为 4 个反斜杠,一切都会开始正常工作。

string.replace(new RegExp('\\"', 'g'),'"');

The result:

结果:

hi \" | hi " 

Why are four backslashes needed as the first parameter of the new RegExp constructor? Why doesn't it work with only 2 of them?

为什么需要四个反斜杠作为新的 RegExp 构造函数的第一个参数?为什么它不能只与其中的 2 个一起使用?

回答by T.J. Crowder

The problem is that you're using the RegExpconstructor, which accepts a string, rather than using a regular expression literal. So in this line in your unescape:

问题是您使用的RegExp是接受字符串的构造函数,而不是使用正则表达式文字。所以在你的 unescape 中的这一行中:

return string.replace(new RegExp('\"', 'g'),'"');

...the \\is interpreted by the JavaScript parser as part handling the string, resulting in a singlebackslash being handed to the regular expression parser. So the expression the regular expression parser sees is \". The backslash is an escape character in regex, too, but \"doesn't mean anything special and just ends up being ". To have an actual backslash in a regex, you have to have two of them; to do that in a string literal, you have to have four(so they survive both layers of interpretation).

...\\被 JavaScript 解析器解释为处理string 的一部分,导致将单个反斜杠传递给正则表达式解析器。所以正则表达式解析器看到的表达式是\". 反斜杠也是正则表达式中的转义字符,但\"并不意味着任何特殊的东西,最终只是". 要在正则表达式中有实际的反斜杠,您必须有两个;要在字符串文字中做到这一点,您必须有四个(这样它们才能在两个解释层中幸存下来)。

Unless you have a very good reason to use the RegExpconstructor (e.g., you have to use some varying input), always use the literal form:

除非您有充分的理由使用RegExp构造函数(例如,您必须使用一些不同的输入),否则请始终使用文字形式:

var utility = {
    escapeQuotes: function(string) {
        return string.replace(/"/g, '\"');
    },
    unescapeQuotes: function(string) {
        return string.replace(/\"/g, '"');
    }
};

It's a lot less confusing.

它少了很多混乱。