Regex Not Capturing Nested JSON Objects in JavaScript - implementing Depth
Hey everyone, I'm running into an issue that's driving me crazy. I'm sure I'm missing something obvious here, but I'm trying to extract nested JSON objects from a string in JavaScript using regex, but I'm running into issues where the regex fails to capture deeper levels of nesting... My input string looks like this: ```javascript const jsonString = '{ "user": { "name": "John", "age": 30, "address": { "city": "New York", "zip": "10001" } }, "active": true }'; ``` I want to extract the `address` object specifically. I started with the following regex pattern: ```regex "address":\s*\{([^}]*)\} ``` However, this only seems to capture the first level of the address object. When I try to include the nested fields, it throws off my match. If I adjust my regex to account for nested braces, like this: ```regex "address":\s*\{([\s\S]*?)\} ``` I still don't get the expected result. Instead, I get the entire string from the first opening brace to the last closing brace, which is not what I need. I'm using JavaScript ES6 syntax and trying to keep the regex as simple as possible for readability, but I need to seem to get around the scenario of nested objects. The output I want should be: ```json { "city": "New York", "zip": "10001" } ``` I've looked into using JSON parsers, but due to the requirement of using regex for this specific scenario, I need to figure it out this way. Any help would be greatly appreciated! This is part of a larger API I'm building. I'm using Javascript 3.9 in this project. Any help would be greatly appreciated! I'm working in a Windows 10 environment. What's the correct way to implement this?