-2

Consider the following example

<pre>
    {
      "cccccc": {},
      "aaaaaa": {
        "xxxxxxxx": true
      },
      "bbbbbbb": {
        "yyyyy": true,
        "zzzzzzzz": true
      }
    }
</pre>

I can select x y and z and assign an action function to them

re = /"(.*?)(": (true|false))/g
hl = '"<span style="color:blue;" onclick="action(\'aaaaaa\',\'$1\')">$1</span>$2';
s = s.replace(re, hl)

But I failed to figure out how to regex aaaaaa or bbbbbbb depending on its position into the first action argument that is now fixed to aaaaaa and should be bbbbbbb for y or z

Gert Cuykens
  • 6,845
  • 13
  • 50
  • 84
  • You are looking for `JSON.parse`, not a regex. – Bergi Jun 15 '15 at 00:47
  • Actually I generate a text interface like this :) I think it is more work to generate a interactive pretty print the other way around starting from a JSON object? – Gert Cuykens Jun 15 '15 at 00:52
  • Where are you getting `aaaaaa` from in `action`? How is this supposed to work? – Explosion Pills Jun 15 '15 at 00:58
  • For now i just use a fixed string `aaaaaa` for all interactive clicks simple because I can't figure out how to make it dynamic like so `action(\'$1\',\'$2\')` – Gert Cuykens Jun 15 '15 at 01:06
  • 2
    @GertCuykens: No, I can assure you that using fragile (regex) text replacements will be more work than a simple programmatical creation. – Bergi Jun 15 '15 at 01:11
  • Probably true, but I am still wondering if it would be possible using regex. If not then it going to be like you suggest. – Gert Cuykens Jun 15 '15 at 01:14
  • 1
    Given that JSON is not a regular language, there is no generic solution at least. – Bergi Jun 15 '15 at 01:27
  • 1
    @GertCuykens I find it frustrating that you've received a slew of down votes for, "Wondering if it would be possible using regex." These down votes are presumably declaring the folly of trying to use a regex to parse this input. I agree with that sentiment, but I disagree with such down voting based on that sentiment. Have a +1 to offset. – Jonathan Mee Jun 15 '15 at 11:16
  • Changed the title so it is more clear that I just want to find out if it would be possible. – Gert Cuykens Jun 16 '15 at 00:50

1 Answers1

-1

You could do this in a two step regex if you so desire:

[^"]*"((?:[^\\"]|\\.)*)"\s*:\s*{(.*)}

Your odd matches are your keys, and your even matches are your values. You'd need to run a secondary regex on your even matches to find all your values, something like:

[^"]*"((?:[^\\"]|\\.)*)"\s*:\s*(?:true|false)

Which should extract all your values.

EDIT:

You cannot accomplish this with only a single regex and here's why, if you capture each variable in quotes you'll end up with 6 captures:

  1. cccccc
  2. aaaaaa
  3. xxxxxxxx
  4. bbbbbbb
  5. yyyyy
  6. zzzzzzzz

It's completely unclear what any of these are without a visual inspection of the input. The only way to define a variable as contained by another variable is to extract only the variable name then only the contents therein the meter gives defines what the capture is. My first capture would give you:

  1. cccccc

  2. aaaaaa
  3. "xxxxxxxx": true
  4. bbbbbbb
  5. "yyyyy": true, "zzzzzzzz": true

JavaStript does not currently support repetitive captures. But even if it did think about the complexity of combining my two regexes into one:

[^"]*"((?:[^\\"]|\\.)*)"\s*:\s*{((?:[^"]*"((?:[^\\"]|\\.)*)"\s*:\s*{(.*)})*)}

You'd end up with the following matches:

  1. cccccc

  2. aaaaaa
  3. "xxxxxxxx": true
  4. xxxxxxxx
  5. bbbbbbb
  6. "yyyyy": true, "zzzzzzzz": true
  7. yyyyy
  8. zzzzzzzz

You'd have to process the whole set of captures, identifying captures as contents. You could do that by finding any empty capture or any capture containing a '"' character. Then you could assume that every variable immediately preceding a capture of contents was a containing variable, and every variable succeeding a capture of contents was a contained variable; up to the next containing variable.

Of course aside from creating a lot of extra work, as I linked, this isn't currently possible in JavaScript regexes. So I'll return to my original answer's statement: As Bergi mentioned avoidance of regexes in favor of a programatic solution, JSON.parse or similar, should always be preferred because of the fragility of regexes.

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • Sorry I didn't downvote, somebody else did I appreciate your help, PS I can only do one regex with s.replace, using a second replace would make it even more complicated I think. – Gert Cuykens Jun 15 '15 at 02:42
  • @Downvoter Some clarification on the reason for the downvote would be very helpful. – Jonathan Mee Jun 15 '15 at 10:34
  • 1
    @GertCuykens Unfortunately if you just need to do it in one regex that's not going to be possible in JavaScript. I've edited my answer with an explanation of why you must use two regexes. – Jonathan Mee Jun 15 '15 at 11:12
  • 1
    Thank you, accepted answer and upvoted trying to compensate the anonymous downvoters – Gert Cuykens Jun 16 '15 at 00:47