title: Character replacement mask over a string in javascript
body: <p>I need to apply a replacement mask over a string in javascript. The mask is a user input with the following syntax:</p>
  
  <ul>
  <li>'#' indicates that the character at the same position in the input string should be left untouched</li>
  <li>Any other value indicates that the character at the same position in the input string should be replace by this value</li>
  </ul>
  
  <p>I have come up with the following code that appears to be working but I am wondering if there is a better way to achieve this with a single regular expression or any other way (no library please).</p>
  
  <p>Thank you </p>
  
  <pre><code>var reference = '123-45678-000';
  var mask ='###W#####-9##';
  var newReference = mask;
  
  while ((match = /#{1}/.exec(newReference)) != null) {
     newReference =  newReference.substring(0, match.index) + reference.substring(match.index,match.index+1) + newReference.substring(match.index + 1);
  }
  console.log("old : " +  reference);      //prints 123-45678-000
  console.log("mask: " +  mask);           //prints ###W######9##
  console.log("new : " +  newReference);   //prints 123W45678-900
  </code></pre>