1

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
2
  • Are you generating javascript code using javascript code? My GOD i hope not... Commented 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. Commented Mar 2, 2011 at 12:41

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

2

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

4 Comments

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.
@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.
I've tried your example and one with double quotes.. hasn't fixed it
@Skizit: Apparently the initial string was also single-quoted.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.