17

I'm trying to change HTTP response body with Charles Proxy using rewrite tool / regex? The response is a JSON.

So part of the returned response JSON body is:

"unavailablePosts": ["AA", "BB"],

and what I want is:

"unavailablePosts": "XXX",

I try to set up Charles' rewrite like this:

enter image description here

So the regex is like:

"unavailablePosts": \[(.*)\],

But ... (as I'm asking this question) it does not work, i.e. nothing is changed in response body.

9
  • What do you mean by "does not work"? (IMHO, SO should not let post questions with this phrase.) You should say what it does not match and what it should match. Nov 4, 2016 at 10:16
  • sorry, updated. Nov 4, 2016 at 10:18
  • Perhaps, there are whitespaces in between, but only you can check that. Try "unavailablePosts"\s*:\s*\[([\s\S]*?)\], Nov 4, 2016 at 10:20
  • with your regex it does not even work in "regex sandbox" like regex101.com Nov 4, 2016 at 10:26
  • 2
    Are you sure? Nov 4, 2016 at 10:30

2 Answers 2

8

I believe there are some linebreaks in between the values, and to match them you need to add \s* (zero or more whitespaces) around : and use [\s\S]*? (any 0+ chars but as few as possible as *? is a lazy quantifier) to match the substring between [ and ],:

 "unavailablePosts"\s*:\s*\[([\s\S]*?)\],
0
2

I had similar issue. In this case Regex is ("unavailablePosts":)"[\s\S]*?" Value for replace is $1"anything"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.