javascript 有文字字符串吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2553738/
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
Does javascript have literal strings?
提问by DevelopingChris
In c# and ruby and many other languages you can denote a string as to not need escaping.
in c# its like this
在 c# 和 ruby 以及许多其他语言中,您可以将字符串表示为不需要转义。
在 c# 中它是这样的
string s = @"\whatever\this\is";
the results are when printed
结果在打印时
\whatever\this\is
my question is, is this supported in any form in javascript?
我的问题是,这在 javascript 中以任何形式支持吗?
回答by Peter Bailey
Short answer: No
简短回答:否
Long answer: Noooooooooooooooooooooooooo
长答案:Nooooooooooooooooooooooooo
回答by Pointy
I don't know what you're getting at, but one way to get around the problem of escaping (etc) is use a trick that John Resig seems to like a lot. You include <script>blocks in a page, but give them a "type" like "text/plain" to make sure that the browser doesn't hand them over to Javascript. Then use the text of the script block for whatever you like.
我不知道你在说什么,但是解决转义(等)问题的一种方法是使用 John Resig 似乎很喜欢的一个技巧。您<script>在页面中包含块,但给它们一个“类型”,如“文本/纯文本”,以确保浏览器不会将它们交给 Javascript。然后将脚本块的文本用于您喜欢的任何内容。
<script id='a_string' type='text/plain'>
Here is some stuff.
There might be some \escape sequences in it.
</script>
Then you can grab that with $('#a_string').text()(or with getElementByIdif you're not using jQuery or something like that).
然后你可以用$('#a_string').text()(或者getElementById如果你不使用jQuery或类似的东西)抓住它。
edit:Here's John Resig's explanation about why dropping stuff into script blocks like that is a good idea:
编辑:这是 John Resig 关于为什么像这样将内容放入脚本块是个好主意的解释:
Quick tip: Embedding scripts in your page that have a unknown content-type (such is the case here - the browser doesn't know how to execute a text/html script) are simply ignored by the browser - and by search engines and screenreaders. It's a perfect cloaking device for sneaking templates into your page. I like to use this technique for quick-and-dirty cases where I just need a little template or two on the page and want something light and fast.
快速提示:在页面中嵌入具有未知内容类型的脚本(这里就是这种情况 - 浏览器不知道如何执行文本/html 脚本)会被浏览器忽略 - 以及搜索引擎和屏幕阅读器. 这是一个完美的隐藏设备,可以将模板潜入您的页面。我喜欢将这种技术用于快速而肮脏的情况,在这种情况下,我只需要页面上的一两个小模板,并且想要轻松快速的东西。
Taken from this page: http://ejohn.org/blog/javascript-micro-templating/
回答by Iain J. Reid
Literal strings are available through the use of ES6 language features. Node v4.x now supports these and around 90% of the other ES6 additions as well.
文字字符串可通过使用 ES6 语言功能获得。Node v4.x 现在支持这些以及大约 90% 的其他 ES6 添加。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
In JS string literals are known as Template Strings. And the syntax is pretty straight forward.
在 JS 中,字符串文字被称为模板字符串。语法非常简单。
回答by plodder
Just escape the escapes
只是逃避逃避
var myCrazyString = "\yes\we\have\no\bananas"
回答by Jonathan Newton
This will work as long as you don't throw a \xinto the string!
只要您不将 a\x放入字符串中,这就会起作用!
var str = String.raw`\whatever\this\is`;
console.log(str);
回答by Jonathan Newton
I know it is six years late now, but I've got one solution to this ;)
我知道现在已经晚了六年,但我有一个解决方案;)
function literalString(regex) {
return ('' + regex).slice(1, -1);
};
O.innerHTML = literalString(/\whatever\this\is/);
<pre id=O>
You basically convert a regex to string and removes the first and last characters.
您基本上将正则表达式转换为字符串并删除第一个和最后一个字符。

