最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Escape character in javascript, Replace " to only " - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question asked Mar 14, 2013 at 12:27 Punith RajPunith Raj 2,1956 gold badges27 silver badges48 bronze badges 9
  • The server response doesn't need any transformations. It's valid JSON. – Ja͢ck Commented Mar 14, 2013 at 12:30
  • What exactly are you doing with the string? Sounds like you're double-escaping somewhere needlessly. – deceze Commented Mar 14, 2013 at 12:31
  • @jack yes its a valid JSON, but my UI is showing it as THIS KEY\" ..... but i want it as THIS KEY" – Punith Raj Commented Mar 14, 2013 at 12:31
  • @deceze THIS KEY" is the value stored in Database, on retrieve and kept it in java String its escaped to THIS KEY\" and then when the object is converted to JSON it is now THIS KEY\\\" – Punith Raj Commented Mar 14, 2013 at 12:33
  • str.replace('\\\\"','\"'); worked. please leave your ment if there is any better ways. – Punith Raj Commented Mar 14, 2013 at 12:38
 |  Show 4 more ments

2 Answers 2

Reset to default 4

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;

You can use replace() function:

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

It works.

P.S. You have forget a "\"

发布评论

评论列表(0)

  1. 暂无评论