I've been trying to replace to following string using the following...
var r = response.replace('var s = getService().getValue(\"join\")', 'null');
However, the String remains un changed and I can't understand why. The String itself takes the following format..
{"r":[],"c":true,"c":{"tags":
[],"":3023,"s":".src.util.S@6f4e9e57","class":"class
src.util.dtos.DTO","Type":"public","c":"m","s":0,"de
fault":false,"id":544,"d":"","n":4,"na":"S","tagString":"","Pages":5},"results":[],"q":"","msg":"var
s = getService().getValue(\"join\")
The actual string itself is a little longer but I hope you get the idea from that abstract.
asked Mar 2, 2011 at 12:36
Skizit
45.2k93 gold badges215 silver badges271 bronze badges
-
Are you generating javascript code using javascript code? My GOD i hope not...Zoidberg– Zoidberg2011年03月02日 12:39:34 +00:00Commented Mar 2, 2011 at 12:39
-
@Zoidberg, as he replaces with null I believe he has a javascript object, possibly some legacy system return value, and tries to remove any method calls to get a JSON object.David Mårtensson– David Mårtensson2011年03月02日 12:41:02 +00:00Commented Mar 2, 2011 at 12:41
2 Answers 2
If your
var s = getService().getValue(\"join\")
part is a JavaScript code inside a JSON string, then you need to quote them again before replacing.
var r = response.replace('var s = getService().getValue(\\"join\\")', 'null');
answered Mar 2, 2011 at 12:52
Thai
11.5k2 gold badges49 silver badges63 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
It's because the double quotes escaped in JSON are not supposed to be escaped inside a single-quoted string.
So, instead of:
var r = response.replace('var joinstakqueries = getService().getValue(\"join\")', 'null');
try:
var r = response.replace('var joinstakqueries = getService().getValue("join")', 'null');
answered Mar 2, 2011 at 12:40
Saul
18k8 gold badges67 silver badges93 bronze badges
4 Comments
Skizit
I need the double quotes tho... they're in the string I'm trying to replace.. I have no access to that String so I can't change it.
Saul
@Skizit: That's fine. The double quotes can stay - just don't escape double quotes inside a single-quote string when doing the replacement. Otherwise it won't match.
Skizit
I've tried your example and one with double quotes.. hasn't fixed it
Saul
@Skizit: Apparently the initial string was also single-quoted.
lang-js