Regex function not working when one key contains the other key
<p>I've got the following regex function:</p>

<pre><code>for(var key in args) {
  var regex = new RegExp('{' + key + '[^}]*}', 'g');
  pattern = pattern.replace(regex, args[key]);
}
</code></pre>

<p>This makes sure when <code>args</code> looks like: </p>

<pre><code>var args: {
  name: 'test',
  id: '12' 
}
</code></pre>

<p>And </p>

<pre><code>pattern = "{name}/{id:[0-9]}"
</code></pre>

<p>When this function is called, <code>pattern</code> will change to <code>test/12</code> </p>

<p>But when <code>pattern</code> look like <code>{name[a-z]}/{name2[0-9]}</code>, <code>name2</code> gets the same value as <code>name</code> when my <code>args</code> look like: </p>

<pre><code>var args: {
  name: 'test',
  name2: '12'
}
</code></pre>

<p>How can I edit my code to make <code>pattern</code> look like <code>test/12</code>?</p>