Reputation: 4350
I'm attempting to serialize a string that contains escaped strings into JSON. I would have imagined that JSON.stringify()
would correctly re-escape those strings and allow me to JSON.parse
it. In a simple case, for example:
JSON.parse(JSON.stringify("\\"))
The output from node is "\". The output from the browser is "\" - it seems the browser (chrome in my case) is not correctly converting the double backslash \\
into \\\\
.
Why is that?
Upvotes: 3
Views: 323
Reputation: 160883
When you write code, you have to write "\\"
(because backslash self is used as escaping), which is a string contains only one backslash ("\\".length
is 1
).
But when displayed in console or browser, it will displayed as "\"
.
Upvotes: 2