ES6模板文字-从字符串中删除\ n [英] ES6 Template Literals - remove \n from the string
问题描述
我将多行变量更改为Template Literals
,这很了不起,但是后来我注意到,我所做的缩进已使用对原始代码所做的缩进转换(最小化)为\n
.我该如何避免呢?
I'm changing my multiline variables to Template Literals
and it's amazing, but then I noticed that the indents I do are converted (minified) into \n
with the indentation I did on the original code. How can I avoid that?
例如:
var $div = $(`<div class='proj' id='projects'>
<div class='bot-nav'>${txt}</div>
</div>`);
已转换为:
var $div = $("<div class='proj' id='projects'>\n <div class='bot-nav'>"+txt+"</div>\n </div>");
我想要这个:
var $div = $("<div class='proj' id='projects'><div class='bot-nav'>"+txt+"</div></div>");
有什么办法吗?
推荐答案
虽然使用.replace
(如其他答案中所建议的)可以工作,但这并不是很酷的新方法;)
While using .replace
(like suggested in other answers) will work, it is not the cool new shiny way of doing it ;)
我认为您正在寻找的是ES2015中引入的文字标记功能(又称标记模板").
I think what you are looking for is a literal tag function (aka "Tagged Templates"), introduced in ES2015.
这里有一堆:
https://github.com/declandewet/common-tags
您可能希望 oneLine
(或oneLineTrim
):
oneLine`
foo
bar
baz
`
// "foo bar baz"
注意:oneLine
显然在内部使用replace
.
Note: oneLine
, obviously, uses replace
internally.
这篇关于ES6模板文字-从字符串中删除\ n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!