在另一个字符串中找到JSON字符串的正则表达式

I would like to extract a JSON string within another string.

Currently I am getting the full string using file_get_contents and running the following pattern on the string: https://regex101.com/r/5jAucO/1 which pretty much gives multiple matches.

I would like to extract the JSON string that is saved in window._sharedData but haven't been able to achieve that. Does someone have any idea how I could do that?

Why not include _sharedData in the regex like? _sharedData\s*=\s*\{.+\}

  • or with lookbehind: (?<=_sharedData\s=)\s*\{.+\}

  • or take the json from a capturing group: _sharedData\s*=\s*(\{.+\})

One concern with the lookbehind is if they add an additional whitespace character between _sharedData and = it won't match.

This only works well since there are no linebreaks in the JSON.