0

I want to keep string values in my insert object as is

This works fine, the \n is kept

{insert: "erferferf\nerferferf"}

This

{insert: "\n\n\n"}

And this becomes a problem

{insert: "\n"}

it makes the string like this

    {insert: "


"}

I want the string to keep the "\n\n\n" rather than becoming a bunch of new lines

1 Answers1

0

Use .replace() to replace \n with \\n. If you leave \n in the variable, it is interpreted as a next line character. So, just replace it with \\n, thus effectively making it \ + n.

Here is an example to illustrate what I'm on about.

string = "javascript\n";
console.log(string.replace("\n","\\n"));
data = {insert:string.replace("\n","\\n")};
console.log("From Object: "+data['insert']);

string = "\n"
console.log(string.replace("\n","\\n"));
data = {insert:string.replace("\n","\\n")};
console.log("From Object: "+data['insert']);