-2

I'm trying to extract a certain js object from a text/js file via regex. The whole file contains a lot of code, but I'm only interested in getting a variable called cfg.

The file could look like this:

function A(){

}

var cfg = {
   setting:1,
   setting2: 5,
   setting3:[],
   setting4:{ 
      setting5:"test"
   }
};

function B(){

}

Could anyone help me to figure out the regex for this ? I'm terrible at regex and I've tried plenty of available solutions but some of them don't seem to be valid in C#. Right now my solution is using:

string[] split = input.Split(new string[] { "var cfg = " }, StringSplitOptions.None);
if(split.Length > 2)
{
   string[] split2 = split[1].Split(new string[] { ";" }, StringSplitOptions.None);
   return JObject.Parse(split2[0]);
}

Which obviously doesn't cover different writings of the cfg object. Like var cfg={};

Thanks in advance!

Rand Random
  • 7,300
  • 10
  • 40
  • 88
omnom22
  • 3
  • 3
  • 5
    Don't use regex for this but a real parser instead, as discussed here for example: https://stackoverflow.com/questions/14355910/javascript-parser-and-analyzer-in-c-sharp-net-4-5 – Flat Eric Nov 24 '17 at 16:14

1 Answers1

0

I would recoment you use a JS parser such as Jint if your code is more complex or will change. But for your example this should work:

var r = new Regex(@"var cfg = (?<Content>\{[^;]*});");
var content = r.Match(text).Groups["Content"];

Note: If the cfg literal contains ; the regex will not work as expected.

Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357