I have a text like this:
123\n456
789
How can I save this text in a variable without loosing the information if the newline was made with \n or with a existing newline?
If I save it with template strings like this:
var str = `123\n456
789`;
the saved variable will be 123\n456\n789. Is there a way to differentiate between the two?
How can I save this text in a variable, such as a string containing the sequence
\nand not a line break?
You must escape the backslash in the string literal:
var str = "\n" or you can use a template literal tagged with String.raw :
var str = String.raw("\n");Is this what you are looking for?
var str = `123\\n456 789`; console.log(str);