-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!

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.