title: Insert commas into number string
body: <p>Hey there, I'm trying to perform a backwards regular expression search on a string to divide it into groups of 3 digits. As far as i can see from the AS3 <a href="http://livedocs.adobe.com/flex/3/html/help.html?content=12_Using_Regular_Expressions_01.html" rel="noreferrer">documentation</a>, searching backwards is not possible in the reg ex engine. </p>
  
  <p>The point of this exercise is to insert triplet commas into a number like so:</p>
  
  <pre><code>10000000 =&gt; 10,000,000
  </code></pre>
  
  <p>I'm thinking of doing it like so:</p>
  
  <pre><code>string.replace(/(\d{3})/g, ",$1")
  </code></pre>
  
  <p>But this is not correct due to the search not happening from the back and the replace $1 will only work for the first match.</p>
  
  <p>I'm getting the feeling I would be better off performing this task using a loop.</p>
  
  <p>UPDATE:</p>
  
  <p>Due to AS3 not supporting lookahead this is how I have solved it.</p>
  
  <pre><code>public static function formatNumber(number:Number):String
  {
      var numString:String = number.toString()
      var result:String = ''
  
      while (numString.length &gt; 3)
      {
          var chunk:String = numString.substr(-3)
          numString = numString.substr(0, numString.length - 3)
          result = ',' + chunk + result
      }
  
      if (numString.length &gt; 0)
      {
          result = numString + result
      }
  
      return result
  }
  </code></pre>