title: JavaScript recursive bracket matching
body: <p>First of all, I've included a jsFiddle highlighting what I'm trying to do, found here: <a href="http://jsfiddle.net/6Vbgd/3/" rel="nofollow">http://jsfiddle.net/6Vbgd/3/</a></p>
  
  <p>I'm trying to do dynamic calculation parsing in JavaScript, which works great if you enter calculations such as <code>SUM(Sales) + SUM(Revenue)</code>, no problem.</p>
  
  <p>However, <code>SUM(Sales) * SUM(Revenue) + SUM(Sales)</code> will work (due to my recursive algorithm) work right to left, so will do <code>SUM(Revenue) + SUM(Sales)</code> first. The way around this is to get the users to enter brackets to order which part of the calculation they want to do first.</p>
  
  <p>For the life of me I can't figure this out... has anyone else done the same? Hopefully the code provided will further demonstrate what I'm trying to do.</p>
  
  <p>Code from there:</p>
  
  <pre><code>var calculation = '(SUM(Sales) * SUM(Revenue)) + SUM(Sales)';
  //var calculation = 'Sales';
  var resorted = [];
  
  // Need to enter into an array into the order of which calculations need to be run first.
  
  // so, it should be...
  // resorted[0] = SUM(Sales) * SUM(Revenue)
  // resorted[1] = + SUM(Sales)
  
  // Then I can parse it such as...
  
  //var calculation = 'SUM(Sales) + (SUM(Sales) + SUM(Revenue))';
  
  // Then... (as my JS calculations run from right to left)
  
  //var calculation = 'SUM(Sales) + SUM(Sales) * SUM(Revenue)';
  </code></pre>