℡Wang Yan 2009-04-30 02:11 采纳率: 100%
浏览 583
已采纳

在 JavaScript 中创建多行字符串

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

转载于:https://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript

  • 写回答

30条回答 默认 最新

  • 撒拉嘿哟木头 2009-04-30 02:15
    关注

    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.

    A template literal is delimited by backticks:

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

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

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


    Original ES5 answer:

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

    "foo \
    bar"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(29条)

报告相同问题?

悬赏问题

  • ¥15 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页