0

I am using HighCharts and am generating script from C# and there's an unfortunate thing where they use inline functions for formatters and events. Unfortunately, I can't output JSON like that from any serializer I know of. In other words, they want something like this:

"labels":{"formatter": function() { return Highcharts.numberFormat(this.value, 0); }}

And with my serializers available to me, I can only get here:

"labels":{"formatter":"function() { return Highcharts.numberFormat(this.value, 0); }"}

These are used for click events as well as formatters, and I absolutely need them.

So I'm thinking regex, but it's been years and years and also I was never a regex wizard.

What kind of Regex replace can I use on the final serialized string to replace any quoted value that starts with function() with the unquoted version of itself? Also, the function itself may have " in it, in which case the quoted string might have \" in it, which would need to also be replaced back down to ".

I'm assuming I can use a variant of the first answer here:

Finding quoted strings with escaped quotes in C# using a regular expression

but I can't seem to make it happen. Please help me for the love of god.

I've put more sweat into this, and I've come up with

    serialized = Regex.Replace(serialized, @"""function\(\)[^""\\]*(?:\\.[^""\\]*)*""", "function()$1");

However, my end result is always:

formatter:function()$1

This tells me I'm matching the proper stuff, but my capture isn't working right. Now I feel like I'm probably being an idiot with some C# specific regex situation.

Update: Yes, I was being an idiot. I didn't have a capture around what I really wanted.

   `enter code here` serialized = Regex.Replace(serialized, @"""function\(\)([^""\\]*(?:\\.[^""\\]*)*)""", "function()$1");

that gets my match, but in a case like this:

"formatter":"function() { alert(\"hi!\"); return Highcharts.numberFormat(this.value, 0); }"

it returns:

"formatter":function() { alert(\"hi!\"); return Highcharts.numberFormat(this.value, 0); }

and I need to get those nasty backslashes out of there. Now I think I'm truly stuck.

Community
  • 1
  • 1
Kurt Koller
  • 446
  • 3
  • 22

3 Answers3

1

Regexp for match

"function\(\) (?<code>.*)"

Replace expression

function() ${code}
Michal Franc
  • 1,036
  • 10
  • 16
1

Try this : http://regexr.com?30jpf

What it does :

Finds double quotes JUST before a function declaration and immediately after it.

Regex :

(")(?=function()).+(?<=\})(")

Replace groups 1 & 3 with nothing :

3 capturing groups: 
   group 1: (")
   group 2: ()
   group 3: (")
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • Does this take escaped quotes into consideration? – Kurt Koller Apr 10 '12 at 21:20
  • @InsidiousForce What do you mean? `\"` WITHIN `"function() { return Highcharts.numberFormat(this.value, 0); }"`? – Dr.Kameleon Apr 10 '12 at 21:21
  • yeah they will be there sometimes, " will have been escaped as \" by the serializer. – Kurt Koller Apr 10 '12 at 21:22
  • @InsidiousForce Just checked it. No problem at all if there is an escaped quote within `"function() { return Highcharts.numberFormat(this.value, 0); }"`. Have a look : http://regexr.com?30jpi – Dr.Kameleon Apr 10 '12 at 21:22
  • @InsidiousForce Just make sure to replace group 1 and 3, just these. I'd write the exact code needed, but honestly it's been a while since I've played with C# and I'm not going to take my chances... – Dr.Kameleon Apr 10 '12 at 21:27
  • See my new update - I need to make sure that anything inside is unescaped, your code matches all of it but the result is same as mine, nothing unescaped. right? – Kurt Koller Apr 10 '12 at 21:32
  • @InsidiousForce Have you tried it out? (I'm a Mac user, so give one min to install Mono; and I'll let you know) - also, could you post a bigger sample (of what you want to convert, I mean) so that I can run my test against it? – Dr.Kameleon Apr 10 '12 at 21:41
  • 1
    @InsidiousForce Also, if the escaped quotes is your only issue, why don't you just replace `\"` with `"` in your final string? – Dr.Kameleon Apr 10 '12 at 21:49
  • that makes sense, and that is what I did. was trying to do it all in one regex, just got locked on that idea and got stuck. thanks for your help. – Kurt Koller Apr 17 '12 at 00:06
1
    string serialized = JsonSerializer.Serialize(chartDefinition);
    serialized = Regex.Replace(serialized, @"""function\(\)([^""\\]*(?:\\.[^""\\]*)*)""", "function()$1").Replace("\\\"", "\"");
Kurt Koller
  • 446
  • 3
  • 22