模板字符串ES6防止换行 [英] Template Strings ES6 prevent line breaks

查看:986
本文介绍了模板字符串ES6防止换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长字符串,该字符串是使用ES6模板字符串构建的,但我希望它没有换行符:

I have a long string, which I build using ES6 template strings, but I want it to be without line breaks:

var string = `As all string substitutions in Template Strings are JavaScript
              expressions, we can substitute a lot more than variable names.
              For example, below we can use expression interpolation to 
              embed for some readable inline math:`

console.log(string);

结果:

As all string substitutions in Template Strings are JavaScript
expressions, we can substitute a lot more than variable names.
For example, below we can use expression interpolation to
embed for some readable inline math:

我的期望:

As all string substitutions in Template Strings are JavaScript expressions, we can substitute a lot more than variable names. For example, below we can use expression interpolation to embed for some readable inline math:

推荐答案

这太疯狂了.

此处几乎每个答案都建议运行一个功能运行时,以便对格式正确的 buildtime 格式错误的文本进行操作.我是唯一一个对此感到震惊的人,尤其是对性能的影响?

Almost every single answer here suggest running a function runtime in order to well-format, buildtime bad-formatted text oO Am I the only one shocked by that fact, especially performance impact ???

如@dandavis所述,官方解决方案(这也是unix shell脚本的历史性解决方案)是使用转义字符\

As stated by @dandavis, the official solution, (which btw is also the historic solution for unix shell scripts), is to escape the carriage return, well, with the escape character : \

`foo \
bar` === 'foo bar'

在此过程中简单,高效,正式,易读且类似于shell

Simple, performant, official, readable, and shell-like in the process

这篇关于模板字符串ES6防止换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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