title: C# - Read JavaScript file and access variable
body: <p>I have a JavaScript file with variables defined and values assigned to them. Is it possible to read the .js file with C# and easily retrieve the variables and values? I know what the variable names will be, but the values will be a json string. I'm hoping I don't have to resort to regular expressions.</p>
  
  <p>EDIT: I do not have control over the contents of the JavaScript file. A third party drops the file to a location and I need to pick it up, read it, and parse it.</p>
  
  <p>EDIT 2: Ok, forget I mentioned json. That actually has nothing to do with my question...which at this point doesn't seem to have a simple solution. So I went with reading the .js file line by line, searching for the variable name, and getting the value. Code below for reference.</p>
  
  <pre><code>using (StreamReader r = new StreamReader("myFile.js"))
  {
    while(r.Peek() &gt;= 0)
      {
        var line = r.ReadLine();
        if(line.IndexOf("myVariableName") &gt; -1)
        {
           var arr = line.split("=");
           var variable = arr[0].Trim();
           var value = arr[1].Trim();
        }
      }
  }
  </code></pre>