如何在node.js中执行多行字符串? [英] How do I do a multi-line string in node.js?

查看:344
本文介绍了如何在node.js中执行多行字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着node.js的兴起,JavaScript中越来越需要多行字符串.

With the rise of node.js, multi-line strings are becoming more necessary in JavaScript.

  1. 即使在浏览器中不起作用,在Node.JS中是否有一种特殊的方法?
  2. 是否有我可以支持的计划或至少一项功能请求?

我已经知道您可以在每一行的末尾使用\n\,这不是我想要的.

I already know that you can use \n\ at the end of every line, that is not what I want.

推荐答案

节点v4和节点的当前版本

从ES6(以及大于v4的Node版本)开始,向Javascript添加了新的模板文字"固有类型(以反引号`"表示),该固有类型也可用于构造多行字符串,如:

node v4 and current versions of 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:

`this is a 
single string`

其计算结果为:'this is a\nsingle string'.

请注意,第一行末尾的换行符包含在结果字符串中.

添加了模板文字,以使程序员可以构造字符串,而无需使用util.format或其他模板程序,就可以将值或代码直接注入到字符串文字中,例如:

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.format or other templaters, as in:

let num=10;

console.log(`the result of ${num} plus ${num} is ${num + num}.`);

将显示"10加10的结果为20".到控制台.

which will print "the result of 10 plus 10 is 20." to the console.

旧版本的节点可以使用行连续"字符,从而允许您编写多行字符串,例如:

Older version of node can use a "line continuation" character allowing you to write multi-line strings such as:

'this is a \
single string'

,其计算结果为:'this is a single string'.

请注意,结果字符串中包含第一行末尾的换行符.

Note that the newline at the end of the first line is not included in the resulting string.

这篇关于如何在node.js中执行多行字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆