0

JSON: {"name":"firstName\nSecondName"}

I am trying to format this JSON, to print:

{
  "name": "firstName\nSecondName"
}

Attempt 1:

token.replace(/\n|\b|\f|\r|\t|\v|\0|\\/, "\\$&") 

Attempt 2:

token.replace(/\\[bfnrtv0'"\\]/, "\\$&") /* This regexp doesn't work at all */

(all formatting implementation code not shown here)

When it comes to printing literal \n, my code process.write.stdout(token) prints a new line instead:

{
  "name": "firstName
  SecondName"
}

How can I print reserved escape sequence literally in JS?

HarshvardhanSharma
  • 754
  • 2
  • 14
  • 28

0 Answers0