title: Enhancing regex of thousands separator?
body: <p>I saw this beautiful script to add thousands separator to js numbers: </p>
  
  <pre><code>function thousandSeparator(n, sep)
  {
      var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})'),
          sValue = n + '';
      if(sep === undefined)
      {
          sep = ',';
      }
      while(sRegExp.test(sValue))
      {
          sValue = sValue.replace(sRegExp, '$1' + sep + '$2');
      }
      return sValue;
  }
  </code></pre>
  
  <p>usage : </p>
  
  <pre><code>thousandSeparator(5000000.125, '\,') //"5,000,000.125"
  </code></pre>
  
  <p>However I'm having a trouble accepting the <em>while loop</em>.</p>
  
  <p>I was thinking to change the regex to : <code>'(-?[0-9]+)([0-9]{3})*'</code> <em>asterisk</em>...</p>
  
  <p>but now , how can I apply the replace statement ?</p>
  
  <p>now I will  have <code>$1</code> and <code>$2..$n</code></p>
  
  <p>how can I enhance the replace func?</p>
  
  <p><em>p.s. the code is taken from here <a href="http://www.grumelo.com/2009/04/06/thousand-separator-in-javascript/" rel="nofollow">http://www.grumelo.com/2009/04/06/thousand-separator-in-javascript/</a></em></p>