I've got some Javascript code that I need to parse with Perl:
var materials ={
foo: "bar",
bar: "baz",
baz: "foo"
},
I've got this Javascript variable as a string, and I want to match the associative array's body so that I can parse it as JSON with Perl using parse_json(). I cannot figure out what I'm doing wrong with my regular expression:
my ($json_str) = $js_code =~ m/var\smaterials\s=\s+({.+}),/i;
$json_str ends up being uninitialized.
\s*instead of requiring\s+). However, JSON can be more complex than this example. It might be easier to just drop the portion you do not want:s/^[^{]+//. This will strip everything that comes before the first{.JSON.stringify(materials)will give you real JSON to start with, which makes everything a whole lot easier.