title: iOS Swift how to parse HTML file with <script> tag and get value inside
body: <p>I have a HTML string like this: </p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;script&gt;
  window.user = {"username":"admin", "meta":{"city": "Los Angeles", "country": "US"}}
  window.flash = {"success": "", "warning": "you cannot do this"}
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I am implementing a function</p>
  
  <pre><code>func dictionaryWithHtml(html: String, variable: String) -&gt; [String: AnyObject]? {
  
  }
  </code></pre>
  
  <p>usage: </p>
  
  <p>calling <code>dictionaryWithHtml(html, variable: "window.user")</code> </p>
  
  <p>should return the dictionary </p>
  
  <p><code>{"username":"admin", "meta":{"city": "Los Angeles", "country": "US"}}</code> </p>
  
  <p>I am trying to search <code>window.user =</code> substring as <code>index1</code>, then find <code>window.flash =</code> substring as <code>index2</code>, then I can easily find the range of JSON for user dictionary. </p>
  
  <p>For <code>window.flash</code>, I could find <code>&lt;/script&gt;</code> substring, and get the range similarly, but what if the server updates and add more variables in the tag? or if server changes the order of assignment. </p>
  
  <p>So is there any better and more robust approach? </p>