如何在 node.js 中创建多行字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6220420/
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 do I do a multi-line string in node.js?
提问by Bryan Field
With the rise of node.js, multi-line strings are becoming more necessary in JavaScript.
随着 node.js 的兴起,多行字符串在 JavaScript 中变得越来越必要。
- Is there a special way to do this in Node.JS, even if it does not work in browsers?
- Are there any plans or at least a feature request to do this that I can support?
- 在 Node.JS 中有没有一种特殊的方法可以做到这一点,即使它在浏览器中不起作用?
- 是否有任何计划或至少是我可以支持的功能请求来执行此操作?
I already know that you can use \n\at the end of every line, that is not what I want.
我已经知道你可以\n\在每一行的末尾使用,这不是我想要的。
回答by Rob Raisch
node v4 and current versions of node
node v4 和 node 的当前版本
As of ES6 (and so versions of Node greater than v4), a new "template literal" intrinsic type was added to Javascript (denoted by back-ticks "`") which can also be used to construct multi-line strings, as in:
从 ES6(以及高于 v4 的 Node 版本)开始,Javascript 中添加了一个新的“模板文字”固有类型(用反引号“`”表示),它也可用于构造多行字符串,如:
`this is a
single string`
which evaluates to: 'this is a\nsingle string'.
其计算结果为:'this is a\nsingle string'。
Note that the newline at the end of the first line isincluded in the resulting string.
请注意,在第一行的末尾的换行符被包括在生成的字符串英寸
Template literals were added to allow programmers to construct strings where values or code could be directly injected into a string literal without having to use util.formator other templaters, as in:
添加了模板字面量以允许程序员构造字符串,其中值或代码可以直接注入字符串字面量而无需使用util.format或其他模板器,如下所示:
let num=10;
console.log(`the result of ${num} plus ${num} is ${num + num}.`);
which will print "the result of 10 plus 10 is 20." to the console.
它将打印“10 加 10 的结果是 20”。到控制台。
Older versions of node
旧版本的节点
Older version of node can use a "line continuation" character allowing you to write multi-line strings such as:
旧版本的节点可以使用“行继续”字符,允许您编写多行字符串,例如:
'this is a \
single string'
which evaluates to: 'this is a single string'.
其计算结果为:'this is a single string'。
Note that the newline at the end of the first line is notincluded in the resulting string.
请注意,第一行末尾的换行符不包含在结果字符串中。
回答by Vijey
Multiline strings are a current part of JavaScript(since ES6) and are supported in node.js v4.0.0 and newer.
多行字符串是 JavaScript(自 ES6 起)的当前部分,并且在 node.js v4.0.0 和更新版本中受支持。
var text = `Lorem ipsum dolor
sit amet, consectetur
adipisicing
elit. `;
console.log(text);
回答by Robert
What exactly are you looking for when you mean multiline strings.
当您指的是多行字符串时,您究竟在寻找什么。
Are you looking for something like:
您是否正在寻找类似的东西:
var str = "Some \
String \
Here";
Which would print as "Some String Here"?
哪个会打印为“这里有一些字符串”?
If so, keep in mind that the above is valid Javascript, but this isn't:
如果是这样,请记住以上是有效的 Javascript,但这不是:
var str = "Some \
String \
Here";
What's the difference? A space after the \. Have fun debugging that.
有什么不同?之后的空格\。祝调试愉快。
回答by David Murdoch
Take a look at the mstringmodule for node.js.
查看node.js的mstring模块。
This is a simple little module that lets you have multi-line strings in JavaScript.
Just do this:
var M = require('mstring')
var mystring = M(function(){/***OntarioMining andForestryGroup***/})to get
mystring === "Ontario\nMining and\nForestry\nGroup"And that's pretty much it.
How It Works
In Node.js, you can call the.toStringmethod of a function, and it will give you the source code of the function definition, including any comments. A regular expression grabs the content of the comment.Yes, it's a hack. Inspired by a throwaway comment from Dominic Tarr.
这是一个简单的小模块,可让您在 JavaScript 中拥有多行字符串。
只需这样做:
var M = require('mstring')
var mystring = M(function(){/***OntarioMining andForestryGroup***/})要得到
mystring === "Ontario\nMining and\nForestry\nGroup"仅此而已。
它是如何工作的
在 Node.js 中,你可以调用.toString一个函数的方法,它会给你函数定义的源代码,包括任何注释。正则表达式获取评论的内容。是的,这是一个黑客。灵感来自Dominic Tarr的一次性评论。
note: The module (as of 2012/13/11) doesn't allow whitespace before the closing ***/, so you'll need to hack it in yourself.
注意:模块(截至 2012/13/11)在结束前不允许有空格***/,所以你需要自己修改它。
回答by Josh
As an aside to what folks have been posting here, I've heard that concatenation can be much faster than join in modern javascript vms. Meaning:
除了人们在这里发布的内容之外,我听说串联比加入现代 javascript vms 快得多。意义:
var a =
[ "hey man, this is on a line",
"and this is on another",
"and this is on a third"
].join('\n');
Will be slower than:
将慢于:
var a = "hey man, this is on a line\n" +
"and this is on another\n" +
"and this is on a third";
In certain cases. http://jsperf.com/string-concat-versus-array-join/3
在某些情况下。http://jsperf.com/string-concat-versus-array-join/3
As another aside, I find this one of the more appealing features in Coffeescript. Yes, yes, I know, haters gonna hate.
另一方面,我发现这是Coffeescript 中更吸引人的功能之一。是的,是的,我知道,讨厌的人会讨厌。
html = '''
<strong>
cup of coffeescript
</strong>
'''
Its especially nice for html snippets. I'm not saying its a reason to use it, but I do wish it would land in ecma land :-(.
它特别适合 html 片段。我并不是说这是使用它的理由,但我确实希望它能够登陆 ecma 地 :-(。
Josh
乔希
回答by Ricardo Tomasi
Take a look at CoffeeScript: http://coffeescript.org
看看 CoffeeScript:http: //coffeescript.org
It supports multi-line strings, interpolation, array comprehensions and lots of other nice stuff.
它支持多行字符串、插值、数组推导和许多其他不错的东西。
回答by Simon D
If you use io.js, it has support for multi-line strings as they are in ECMAScript 6.
如果您使用 io.js,它支持多行字符串,就像它们在 ECMAScript 6 中一样。
var a =
`this is
a multi-line
string`;
See "New String Methods" at http://davidwalsh.name/es6-iofor details and "template strings" at http://kangax.github.io/compat-table/es6/for tracking compatibility.
有关详细信息,请参阅http://davidwalsh.name/es6-io 上的“新字符串方法”和http://kangax.github.io/compat-table/es6/ 上的“模板字符串”以跟踪兼容性。
回答by suvtfopw
In addition to accepted answer:
除了接受的答案:
`this is a
single string`
which evaluates to: 'this is a\nsingle string'.
其计算结果为:'这是一个\ns单个字符串'。
If you want to use string interpolation but without a new line, just add backslash as in normal string:
如果你想使用字符串插值但没有换行,只需像普通字符串一样添加反斜杠:
`this is a \
single string`
=> 'this is a single string'.
=> '这是一个字符串'。
Bear in mind manual whitespace is necessary though:
请记住,手动空格是必要的:
`this is a\
single string`
=> 'this is asingle string'
=> '这是单个字符串'
回答by Tim Caswell
Vanilla Javascipt does not support multi-line strings. Language pre-processors are turning out to be feasable these days.
Vanilla Javascipt 不支持多行字符串。如今,语言预处理器变得可行。
CoffeeScript, the most popular of these has this feature, but it's not minimal, it's a new language. Google's traceur compiler adds new features to the language as a superset, but I don't think multi-line strings are one of the added features.
CoffeeScript,其中最流行的有这个功能,但它不是最小的,它是一种新语言。Google 的 traceur 编译器将新功能作为超集添加到该语言中,但我不认为多行字符串是添加的功能之一。
I'm looking to make a minimal superset of javascript that supports multiline strings and a couple other features. I started this little language a while back before writing the initial compiler for coffeescript. I plan to finish it this summer.
我希望制作一个支持多行字符串和其他几个功能的 javascript 的最小超集。在为 coffeescript 编写初始编译器之前,我开始使用这种小语言。我打算在今年夏天完成它。
If pre-compilers aren't an option, there is also the script tag hack where you store your multi-line data in a script tag in the html, but give it a custom type so that it doesn't get evaled. Then later using javascript, you can extract the contents of the script tag.
如果预编译器不是一个选项,那么还有脚本标记 hack,您可以将多行数据存储在 html 的脚本标记中,但给它一个自定义类型,这样它就不会被评估。然后稍后使用javascript,您可以提取脚本标记的内容。
Also, if you put a \ at the end of any line in source code, it will cause the the newline to be ignored as if it wasn't there. If you want the newline, then you have to end the line with "\n\".
此外,如果您在源代码中的任何行的末尾放置一个 \,它将导致换行符被忽略,就好像它不存在一样。如果您想要换行符,那么您必须以“\n\”结束该行。

