Punith Raj
Punith Raj

Reputation: 2195

Escape character in javascript, Replace \" to only "

I have a json object which will be returned from the server side as follows.

{"name":"value which has \" "} for Ex : {"Key":"This Key\" "}

when i get this response on the client side it is automatically encoded as , result after stringify

 {"Key":"This Key\\\" "}

Now i want to replace the \\\" to only \" so my UI can show only This Key"

Until i tried to do jsonString.replace(/\\\"/g,'\"'); but gives output of This Key\\"

Kindly help me, i have got it wrong..

Regards, Punith

Upvotes: 1

Views: 10167

Answers (2)

Quentin
Quentin

Reputation: 943569

You appear to be trying to write a JSON parser out of regular expressions. Don't do that, use an existing one.

var data = JSON.parse(string_of_json);
var key = data.Key;

Upvotes: 4

nap.gab
nap.gab

Reputation: 451

You can use replace() function:

str.replace('\\\\"','\"');

It works.

P.S. You have forget a "\"

Upvotes: 2

Related Questions