title: Reformatting Strings - Javascript
body: <p>I've been given a task that has to reformat incorrect phone numbers (that are strings) in javascript. The correct output has to be: 123-456-789 or 123-456-789-11 (depending on how many characters there are in the string)? </p>
  
  <p>I have a basic java background and am still getting used to the way javascript works... </p>
  
  <p>Example cases:</p>
  
  <pre><code>00-44 48 5555 8361
  0 - 22 1985--324
  555372654
  </code></pre>
  
  <p>Example cases should ne:</p>
  
  <pre><code>004-448-555-583-61
  022-198-53-24
  555-372-654
  </code></pre>
  
  <p>The code will eventually ignore the dashes and spaces so that correct output can be generated</p>
  
  <p>This is what i have so far:</p>
  
  <pre><code>S = S.replace("", "");
  
  for(var i = 0; i &lt; n; i++) {
      var after = S.replace(/(\d{3})(?=\d)/g, '$1-');
      console.log(after);
  } 
  </code></pre>
  
  <p>Cheers</p>