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!