Javascript 在Javascript中添加双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41908971/
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
Adding double quotes in Javascript
提问by AP_1984
I would like to understand the logic behind that code:
我想了解该代码背后的逻辑:
var myName = "John";
document.write("\""+ myName +"\"");
I got what I wanted, ie broswer displayed "John" with double quotes around, but I don't uderstand why I had to use + before and after the string and why escape seq. had to be used in such manner.
我得到了我想要的东西,即浏览器用双引号显示“John”,但我不明白为什么我必须在字符串前后使用 + 以及为什么要转义 seq。不得不以这种方式使用。
回答by Oriol
Your code is vulnerable to string injection! What if myNamecontains a quote?
您的代码容易受到字符串注入的影响!如果myName包含报价怎么办?
Instead, you should use JSON.stringify
相反,你应该使用 JSON.stringify
var myName = 'John"abc';
'"' + myName + '"'; // "John"abc"
JSON.stringify(myName); // "John\"abc"
You may want to escape U+2028 and U+2029 too.
您可能也想逃避 U+2028 和 U+2029。
回答by Jorge Anzola
The plus sign is used to concate the strings and the backslash is used to tell JS "hey, this is not the ending doble quote mark, I want the actual sign". You can also use both doble and single quote mark in your favor.
加号用于连接字符串,反斜杠用于告诉 JS “嘿,这不是结束双引号,我想要实际的符号”。您也可以同时使用双引号和单引号。
var myName = 'John';
document.write('"' + myName + '"');
回答by Horia Coman
You could have achieved the same with JSON.stringify. Or perhaps "'" + myName + "'", without the escapes.
您可以使用JSON.stringify. 或者也许"'" + myName + "'",没有转义。
The reason you needto do what you did is because the quotes you use when specifying "John"are not actually part of the string. They're a little bit of syntax telling the compiler that whatever is inside them is a string value you specify as a literal (as opposed to reading from the user, or from some other data source). But, the value itself is just John, without the quotes. When you print it, JavaScript will just print it as John. You need to add the quotes or any extra decoration yourself.
您需要做您所做的事情的原因是因为您在指定时使用的引号"John"实际上不是字符串的一部分。它们是一些语法,告诉编译器,它们内部的任何内容都是您指定为文字的字符串值(而不是从用户或其他数据源读取)。但是,值本身只是John,没有引号。当您打印它时,JavaScript 只会将其打印为John. 您需要自己添加引号或任何额外的装饰。
回答by Samuel Toh
The escape character \acts like a hint to the compiler.
转义字符\就像是对编译器的提示。
By specifying the escape the compiler knows that the following "character is a string and it is not suppose to be part of your programming code.
通过指定转义,编译器知道下面的"字符是一个字符串,它不应该是您的编程代码的一部分。
E.g. The compiler will see the string "helloWor\"ld" as "helloWorld".
例如,编译器会将字符串“helloWor\”ld”视为“helloWorld”。
The +operator acts as a string concatenation operator. That is, the compiler will attempt to join the 2 strings into one.
E.g. "he" + getString() + "llo"will give you heXXXllo, assuming the function getString() returns you the value XXX
该+运算符充当字符串连接运算符。也就是说,编译器将尝试将 2 个字符串合并为一个。例如"he" + getString() + "llo"会给你heXXXllo,假设函数 getString() 返回你的值XXX
回答by Viktor
In javascript strings are characters wrapped in either single or double quotes. If you in the string want to use a actual single/double quote you have to "escape" it. This is done with the backslash character. A way around it is to use different wrapping quotes from what you use inside the string. See below
在 JavaScript 中,字符串是用单引号或双引号包裹的字符。如果您在字符串中想要使用实际的单/双引号,则必须“转义”它。这是通过反斜杠字符完成的。一种解决方法是使用与您在字符串中使用的不同的包装引号。见下文
document.write("\"" + myName + "\""); // escaped
document.write("\"" + myName + "\""); // escaped
document.write('" + myName + "'); // not escaped
document.write('" + myName + "'); // not escaped
The plus character is used to concatenate the string. In your example, quote plus variable plus quote which executes to "John"
加号字符用于连接字符串。在您的示例中,引用加上变量加上对“John”执行的引用
回答by Yogesh Kumar Gupta
Please refer to some basic book or w3schools for basics of JS.
JS的基础请参考一些基础书籍或w3schools。
var myName = "John";
Here myName has a value of John without the quotes. the quotes are JS way of recognising any string. Any text within quotes is recognised as String in JS.
这里 myName 的值为 John ,没有引号。引号是 JS 识别任何字符串的方式。引号内的任何文本在 JS 中都被识别为字符串。
Now when you want to display quotes also, you need to make quotes a part of string
现在,当您还想显示引号时,您需要将引号作为字符串的一部分
So "\"" is basically covering (") around quotes. In JS (+) plus sign is use to concatenate two strings.
所以 "\"" 基本上是在引号周围覆盖 (")。在 JS (+) 中,加号用于连接两个字符串。
Hope this clarifies your question!!
希望这能澄清你的问题!!

