title: JavaScript to replace variable in a string
body: <p>I want to write a function to replace variables in a string with actual values, for example:</p>
  
  <pre><code>function replaceUrl(url, data) {}
  </code></pre>
  
  <p>The caller may be:</p>
  
  <pre><code>replaceUrl('/task/:module?taskId=:taskId#:hash', {
      module: 'm1',
      taskId: 't1',
      hash: 'h1'
  });
  </code></pre>
  
  <p>I think to use RegEx may be simple to implement this, but I'm not very familiar with it.</p>
  
  <p>Can anyone provide a simple way on this?</p>