title: Extracting object from string
body: <p>I have a sample script from Webdriver.io website which is something like this (modified for testing)</p>
  <pre class="lang-js prettyprint-override"><code>const { remote } = require('webdriverio');
  var assert = require('assert');
  
  ;(async () =&gt; {
          const browser = await multiremote({
              capabilities: {
                  browserName: 'chrome',
              },
              capabilities: {
                  browserName: 'firefox'
              }
      })
      await browser.url('https://www.google.com');
      var title = await browser.getTitle();
      assert.equal(title, 'Google');
      await browser.deleteSession()
  })()
  </code></pre>
  <p>I need to extract <code>capabilities.browserName</code> from this, and so far I'm able to extract them like this</p>
  <pre class="lang-js prettyprint-override"><code>var browsers = str.match(/\scapabilities\s*?:\s*?\{[^{}]+\}/gi); // find capabilities
  console.log(browsers.length); // check length
  for(let i=0; i &lt; browsers.length; i++){
      let b = browsers[i].replace(/\scapabilities\s*?:\s*?/, '') // remove capabilities: part
                         .replace(/'/g, '&quot;' ) // convert single quote to double quote
                         .replace(/\s/g, '') // remove spaces, new lines, tabs etc.
                         .replace(/(\w+:)|(\w+ :)/g, function(matchedStr) { // wrap words in double quote as keys are not wrapped
                              return '&quot;' + matchedStr.substring(0, matchedStr.length - 1) + '&quot;:';
                            });
  
      let browser = JSON.parse(b); // parse the string to convert to object
      console.log(browser.browserName); // Success !!!
  }
  </code></pre>
  <p>This works fine but some time capabilities can have nested properties like</p>
  <pre class="lang-js prettyprint-override"><code>capabilities: {
     browserName: 'chrome',
     proxy: {
       proxyType: 'PAC',
       proxyAutoconfigUrl: 'http://localhost:8888',
     }
  },
  </code></pre>
  <p>This breaks my code and only returns me one browser <code>firefox</code>. <code>await multiremote()</code> can be <code>await remote()</code> as well.</p>
  <p>Thanks in advance</p>