views:

42

answers:

2

Say i got a variable,

var s = "\"\\\"abc\\\"\"";

which is unescaped to "\"abc\"" when i display in the browser.

But i need it to be further unescaped to the actual value "abc".

A: 

You can, but probably shouldn't, call eval:

s = eval(s);

However, this is extremely dangerous.

For a safer solution, please provide more detail.

SLaks
I'm calling a service which returns me the raw values of a form component in JSON. The value to be displayed in actually "abc" (with quotes), but the JSON returned is "\"\\\"abc\\\"\"". Is the service suppose to return "\"abc\"" instead?var returned = { value: "\"\\\"abc\\\"\""}
A: 

I honestly don't know the context of your problem, but perhaps you can do something like this:-

s = s.replace(/\"|\\/g, ""); // remove all double quote and backslash

Of course, this solution is not going to be fool-proof and may not be the recommended solution if you have to handle more scenarios that are not shown in your example.

limc