Wrap long template literal line to multiline without creating a new line in the string

后端 未结 9 1386
谎友^
谎友^ 2020-12-01 02:40

In es6 template literals, how can one wrap a long template literal to multiline without creating a new line in the string?

For example, if you do this:



        
相关标签:
9条回答
  • 2020-12-01 03:02

    If you introduce a line continuation (\) at the point of the newline in the literal, it won't create a newline on output:

    const text = `a very long string that just continues\
    and continues and continues`;
    console.log(text); // a very long string that just continuesand continues and continues
    
    0 讨论(0)
  • 2020-12-01 03:09

    Similar to Doug's answer this is accepted by my TSLint config and remains untouched by my IntelliJ auto-formatter:

    const text = `a very long string that just ${
      continues
    } and ${continues} and ${continues}`
    
    0 讨论(0)
  • 2020-12-01 03:15

    The solution proposed by @CodingIntrigue is not working for me on node 7. Well, it works if I do not use a line continuation on the first line, it fails otherwise.

    This is probably not the best solution, but it works without problems:

    (`
        border:1px solid blue;
        border-radius:10px;
        padding: 14px 25px;
        text-decoration:none;
        display: inline-block;
        text-align: center;`).replace(/\n/g,'').trim();
    
    0 讨论(0)
提交回复
热议问题