JavaScript 单引号和双引号中的转义字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10699726/
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
Escaping characters in JavaScript single and double quotes
提问by zik
I've got a question about escaping characters in JavaScript that I'm hoping you may be able to help with. Say if I have the following JavaScript code:
我有一个关于在 JavaScript 中转义字符的问题,希望您能提供帮助。假设我有以下 JavaScript 代码:
document.write("<img src=\"http://www.google.com\" />");
Now in the example above, you can see that I have begun the document.write
with double quotes "
and hence why I need to escape the quotes within the <img src="" />
to ensure that JavaScript still thinks that it's a string.
现在在上面的例子中,你可以看到我已经document.write
用双引号开始了,"
因此为什么我需要转义 < 中的引号img src="" />
以确保 JavaScript 仍然认为它是一个字符串。
But in the below example you can see I have used a single quote '
to begin the document.write
statement. My question is do I need to still escape the double quotes? I know that the statement will work without this but what is best practice?
但是在下面的示例中,您可以看到我使用单引号'
来开始document.write
语句。我的问题是我还需要转义双引号吗?我知道没有这个语句也能工作,但最佳实践是什么?
document.write('<img src=\"http://www.google.com\" />');
The reason I ask is I have a conditional statement that I've written that fires off an image (as per the line above) but it doesn't seem to be working and to rule out all possibilities as to what may be causing this. I come across stuff like this pretty much every day so any help would be much appreciated. This may perhaps be a daft question so apologies in advance...
我问的原因是我写了一个条件语句,它触发了一个图像(按照上面的行),但它似乎不起作用,并排除了可能导致这种情况的所有可能性。我几乎每天都会遇到这样的事情,所以任何帮助将不胜感激。这可能是一个愚蠢的问题,所以提前道歉......
回答by Tim
When using single quotes you only need to escape single quotes, not double quotes.
使用单引号时,您只需要转义单引号,而不是双引号。
(EDIT: And vice versa!)
(编辑:反之亦然!)
回答by Elias Van Ootegem
By now, you've got the picture: no need to escape quotes that you're not using as delimiters of that particular string. However: what is best practice is a different story. I know of some people who will tell you that 'always escaping quotes' is a good habit to get into. I disagree. Unlike some other languages JavaScript is reasonably lenient when it comes to escaped characters: in your second example, the backslashes won't be printed out.
到目前为止,您已经了解了情况:无需转义您没有用作该特定字符串的分隔符的引号。然而:最佳实践是另一回事。我知道有些人会告诉你“总是转义引号”是一个很好的习惯。我不同意。与其他一些语言不同,JavaScript 在转义字符方面相当宽松:在您的第二个示例中,不会打印出反斜杠。
This is not always the case, so My suggestion would be: be consistent in which quotes you use (single || double) and escape only the ones that need escaping. Depending on what other languages you're using, you might want to think a bit on which quotes you're going to use. If you're using PHP, for example, stick to single quotes, as double quotes do more than just delimiting a string. If you're used to writing C-like languages (or Java), best stay in the habit of using double quotes, since there is an even bigger difference between single and double quotes in those languages
情况并非总是如此,所以我的建议是:在您使用的引号中保持一致(单||双)并且只转义需要转义的那些。根据您使用的其他语言,您可能需要考虑一下您将使用哪些引号。例如,如果您使用 PHP,请坚持使用单引号,因为双引号不仅仅用于分隔字符串。如果您习惯于编写类 C 语言(或 Java),最好保持使用双引号的习惯,因为这些语言中单引号和双引号之间的差异更大
回答by OptimusCrime
document.write('<img src="http://www.google.com" onClick="foo(\'bar\');" />');
You only need to escape the same kind of quotes that you are using.
您只需要转义您正在使用的相同类型的引号。
回答by MakuraYami
document.write('<img src="http://www.google.com" />');
Will work just fine.
document.write('<img src="http://www.google.com" />');
会工作得很好。
same counts for document.write("<img src='http://www.google.com' />");
相同的计数 document.write("<img src='http://www.google.com' />");
回答by JDev
<html>
<body>
<script type="text/javascript">
document.write(escape("<img src=\"http://www.google.com\" />"));
</script>
</body>
</html>