在JavaScript中创建多行字符串 [英] Creating multiline strings in JavaScript

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

问题描述

我在Ruby中有以下代码。我想将此代码转换为JavaScript。什么是JS中的等价代码?

I have the following code in Ruby. I want to convert this code into JavaScript. what's the equivalent code in JS?

text = <<"HERE"
This
Is
A
Multiline
String
HERE


推荐答案

更新:



ECMAScript 6(ES6)引入了一种新的文字,即 模板文字 。它们有许多功能,其中包括变量插值,但最重要的是,对于这个问题,它们可以是多行的。

Update:

ECMAScript 6 (ES6) introduces a new type of literal, namely template literals. They have many features, variable interpolation among others, but most importantly for this question, they can be multiline.

模板文字由反引号:

var html = `
  <div>
    <span>Some HTML here</span>
  </div>
`;

(注意:我不主张在字符串中使用HTML)

(Note: I'm not advocating to use HTML in strings)

浏览器支持可以,但是您可以使用 transpilers 来提高兼容性。

Browser support is OK, but you can use transpilers to be more compatible.

Javascript没有here-document语法。你可以逃避文字换行,但接近:

Javascript doesn't have a here-document syntax. You can escape the literal newline, however, which comes close:

"foo \
bar"

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

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