title: Regular Expression to add dashes to a phone number in JavaScript
body: <pre><code>function convertToValidPhoneNumber(text) {  
      var result = [];     
      text = text.replace(/^\d{2}-?\d{3}-?\d{3}-?\d{3}$/, "");
      while (text.length &gt;= 6){       
          result.push(text.substring(0, 3));  
          text = text.substring(3);
      }
      if (text.length &gt; 0) result.push(text); 
      return result.join("-");
  }
  </code></pre>
  
  <p>I test this function against <code>35200123456785</code> input string.</p>
  
  <p>It gives me like a number like <code>352-001-234-56785</code> but I want <code>35200-12345678-5</code>.</p>
  
  <p>What do I need to do to fix this?</p>