title: Regex replace format template with a string so that the result is a formatted string
body: <p>Basically I have the following:</p>
  
  <ul>
  <li>input string (eg ABCDEFGHI)</li>
  <li>input template (eg XXX-XXX-XXX)</li>
  </ul>
  
  <p>and the output I want to see is <code>ABC-DEF-GHI</code></p>
  
  <p>I imagine it going something like <code>"XXX-XXX-XXX".replace("regex", "ABCDEFGHI");</code></p>
  
  <p>The catch is that the template is dynamic. It may be <code>XXX-XXX-XXX</code> or <code>XX-XXXX-XXX</code> or any other combination that can include any special character but the charater to match is always <code>X</code>.</p>
  
  <p>The template is not limited in length or number of groups separated by special characters.
  i.e. <code>XX-X-X</code> and <code>XXX-XXX-XXX-XXX-X</code> are both valid templates as long as there are the same number of X's as input characters.</p>
  
  <p>So far I have this: <code>"/^([^a-zA-Z0-9]*X){9}[a-zA-Z0-9]*$/"</code> which will validate my template.</p>
  
  <p>Can anyone shed some light on this? Is there a way to replace one matched character from the template with one character from the string?</p>