replace()method with regular expression
/[\n]/gas first parameter and
\\nstring as second parameter.
Let’s see with the help of example.
const text = 'Line 1 \n Line 2';
Line 1 \n Line 2
As you can see, we have successfully escaped new line using
To escape JSON string containing newline characters, chain
replace() method with all required special characters which you want to escape.