title: TypeScript string replace with regex, groups and partial string
body: <p>I want to use regex to format a number inside an input as I type it.<br>
  My problem is: Since I'm using groups to format the number, it only formats when the string matches the regex.
  Here's an example:<br>
  The full number is: <code>12312312312</code> | Formatted would look like: <code>123.123.123-12</code> .  </p>
  
  <p>If I type <code>1231231</code> for example, it doesn't formats to <code>123.123.1</code> as I was expecting, only if I type the entire number.</p>
  
  <p>This is my function: </p>
  
  <pre><code>format(value){
      // This function returns the formatted string on user input
      return value.replace(/(\d{3})(\d{3})(\d{3})(\d+)/, "\$1.\$2.\$3-\$4");
  }
  </code></pre>
  
  <p>Is there any way to make the remaining groups optionals?</p>