简体   繁体   English

JavaScript 有文字字符串吗?

[英]Does JavaScript have literal strings?

In C# , Ruby , and many other languages you can denote a string as to not need escaping.C#Ruby和许多其他语言中,您可以将字符串表示为不需要转义。 In C# it's like this在 C# 中是这样的

string s = @"\whatever\this\is";

The results are when printed:打印结果如下:

\whatever\this\is

Is this supported in any form in JavaScript?这在 JavaScript 中以任何形式支持吗?

Short answer: No简短回答:否

Long answer: Noooooooooooooooooooooooooo长答案:Nooooooooooooooooooooooooo

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.我不知道你在说什么,但是解决转义(等)问题的一种方法是使用 John Resig 似乎很喜欢的一个技巧。 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.您在页面中包含<script>块,但给它们一个“类型”,如“text/plain”,以确保浏览器不会将它们交给 Javascript。 Then use the text of the script block for whatever you like.然后将脚本块的文本用于您喜欢的任何内容。

<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 getElementById if you're not using jQuery or something like that).然后你可以用$('#a_string').text() (或者如果你不使用 jQuery 或类似的东西,用getElementById来获取它)。

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.快速提示:在页面中嵌入具有未知内容类型的脚本(这里就是这种情况 - 浏览器不知道如何执行文本/html 脚本)会被浏览器忽略 - 以及搜索引擎和屏幕阅读器. 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.我喜欢将这种技术用于快速而肮脏的情况,在这种情况下,我只需要页面上的一两个小模板,并且想要轻松快速的东西。

Taken from this page: http://ejohn.org/blog/javascript-micro-templating/取自此页面: http : //ejohn.org/blog/javascript-micro-templating/

Literal strings are available through the use of ES6 language features.文字字符串可通过使用ES6语言功能获得。 Node.js v4.x now supports these and around 90% of the other ES6 additions as well. Node.js v4.x 现在支持这些以及大约 90% 的其他 ES6 添加。

Template literals (Template strings) 模板文字(模板字符串)

In JavaScript, string literals are known as template strings .在 JavaScript 中,字符串文字被称为模板字符串 And the syntax is pretty straightforward.语法非常简单。

This will work as long as you don't throw a \\x into the string!只要您不将\\x放入字符串中,这就会起作用!

 var str = String.raw`\\whatever\\this\\is`; console.log(str);

只是逃避逃避

var myCrazyString = "\\yes\\we\\have\\no\\bananas"

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 a string and remove the first and last characters.您基本上将正则表达式转换为字符串并删除第一个和最后一个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM