Sandrina Pereira Sandrina Pereira - 10 months ago 47
Javascript Question

ES6 Template Literals - remove \n from the string

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
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>

Is converted to:

var $div = $("<div class='proj' id='projects'>\n <div class='bot-nav'>"+txt+"</div>\n </div>");

And I want this:

var $div = $("<div class='proj' id='projects'><div class='bot-nav'>"+txt+"</div></div>");

Is there any fast/easy way to do this?

Answer Source

While using .replace (like suggested in other answers) will work, it is not the cool new shiny way of doing it ;)

I think what you are looking for is a literal tag function (aka "Tagged Templates"), introduced in ES2015.

There are a bunch of them here:

And you would probably want oneLine (or oneLineTrim):

// "foo bar baz"