-2

How would be the RegEx to make the string

'{"a":"st", "b":"EvalMe(nd)", "c":"th"}'

become

'{"a":"st", "b":nd, "c":"th"}'

?

woz
  • 10,888
  • 3
  • 34
  • 64
EthraZa
  • 388
  • 5
  • 9
  • its no regexp but if its always this pattern, than i would you .replace() – john Smith Apr 01 '14 at 17:36
  • 1
    1) what have you tried? 2) Why use regex? Other than the fact that you're breaking the JSON syntax, is there a reason you can't just parse and manipulate the object directly? – p.s.w.g Apr 01 '14 at 17:36
  • The json was just an example to ilustrate what I need, the real string will be a full Javascript code loaded via Ajax. The "nd" thing need to be a variable, because if I let the code be parsed it will just break, because it is expecting the nd to be an object, not a string. I already know I need to use replace with a regex, but I can't figure out the regex. I'm trying all the day long, but regex just fly over my head. :) – EthraZa Apr 01 '14 at 17:49
  • This, for example: str.replace(/"EvalMe\\(/g,"").replace(/\\)"/g,""); do what I need, but it would be best to be in 1 regex, because the ')"' need to be matched only when it is part of a 'EvalMe('. – EthraZa Apr 01 '14 at 17:55
  • Too lazy to look it up right now, but what you need is a regEx with a capture group. You'll want to look for "EvalMe(" then a capture group of some characters which are *not* ")" and then a single ")". The matched capture group is the substring you're looking for. See for example [here](http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression). – JimmyB Apr 01 '14 at 18:52

2 Answers2

0

It looks like json. So first using json library(not sure how in Javascript) parse this value first "b":"EvalMe(nd)" from the json.

Then from there do a regex replace like below way and then update your json.

output = "EvalMe(nd)".replace(/.*\((.*?)\).*/, "$1");
                                   ^^^^^ picking of nd is here

Here using regex it is capturing the nd in group $1 and replace everything with empty, and keeping the group.

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • Thanks for the answer, but the json is just to simplificate an example. I need it to replace in the full string, like the example I gave in the comment above. – EthraZa Apr 01 '14 at 18:07
0

A simple find and replace is all you need
find: /"EvalMe\(([^()]*)\)"/
replace: \1 or $1