Line break inside string that is inside a string template

Asked

Viewed 1,602 times

0

I know that to break lines inside string, we use the operator \n. I also know that within template strings, we can do it using only the backslash, this way.

var string = `Uma linha \
duas linhas \
três linhas`

See you around, okay. But what if I have a string inside a string template, and I need to break line inside it? Example:

var string2 = `$(this).text(var1 + "\n_________________\n")`

In case I need to pass this line of code inside a template string to another variable that will insert the code directly into the page’s script tag. Using n gives me syntax error, and using the backslash style of the template strings does not skip the line, it just creates a huge space in place of the backslash. How should I proceed?

  • 1

    I couldn’t get it right. You want to pass the Javascript code as a string, and then use something like Eval? I believe there are better ways to do this. The syntax error you mentioned was also unclear, but I guess it’s because the \n is breaking lines. If so maybe you should escape the code with $(this).text(var1 + "\\n_________________\\n")

  • No, I don’t use Eval(). The point is that I need to insert a dynamic table via script into a form, and putting the "out" table script does not work because for some reason it does not recognize any external script. So I create html together a script tag and write the scripts inside that tag. I’ll try what you suggested and return you if it worked, a sec.

  • Use the \\n worked, thank you very much. If you want to write a reply q I give as accepted.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.