Javascript 如何在javascript中的字符串中获取撇号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6257619/
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 get an apostrophe in a string in javascript
提问by webmasters
I'm doing some stuff with javascript and I'm wondering, how do I put an apostrophe in a string in javascript?
我正在用 javascript 做一些事情,我想知道,如何在 javascript 的字符串中放置撇号?
theAnchorText = 'I apostrophe M home';
回答by icktoofay
You can use double quotes instead of single quotes:
您可以使用双引号代替单引号:
theAnchorText = "I'm home";
Alternatively, escape the apostrophe:
或者,转义撇号:
theAnchorText = 'I\'m home';
The backslash tells JavaScript (this has nothing to do with jQuery, by the way) that the next character should be interpreted as "special". In this case, an apostrophe after a backslash means to use a literal apostrophe but not to end the string.
反斜杠告诉 JavaScript(顺便说一下,这与 jQuery 无关)下一个字符应该被解释为“特殊”。在这种情况下,反斜杠后面的撇号意味着使用文字撇号但不结束字符串。
There are also other characters you can put after a backslash to indicate other special characters. For example, you can use \n
for a new line, or \t
for a tab.
您还可以在反斜杠后放置其他字符以指示其他特殊字符。例如,您可以用于\n
换行或\t
制表符。
回答by legendofawesomeness
You can try the following:
您可以尝试以下操作:
theAnchorText = "I'm home";
OR
或者
theAnchorText = 'I\'m home';
回答by Guffa
This is plain Javascript and has nothing to do with the jQuery library.
这是普通的 Javascript,与 jQuery 库无关。
You simply escape the apostrophe with a backslash:
你只需用反斜杠转义撇号:
theAnchorText = 'I\'m home';
Another alternative is to use quotation marks around the string, then you don't have to escape apostrophes:
另一种选择是在字符串周围使用引号,然后您不必转义撇号:
theAnchorText = "I'm home";
回答by FishBasketGordo
You can put an apostrophe in a single quoted JavaScript string by escaping it with a backslash, like so:
您可以通过使用反斜杠将撇号转义到单引号 JavaScript 字符串中,如下所示:
theAnchorText = 'I\'m home';
theAnchorText = 'I\'m home';