title: How can I replace .NET string-placeholders with actual values in javascript?
body: <p>I have a string (coming from ASP.NET) which contains one or more placeholders. In .NET I'm using the string.Format() method to replace the placeholders with actual values:</p>
  
  <pre><code>var str = "aaa {0} bbb {1} ccc {2:0.0}";
  var result = string.Format(str, new [] {1, 2, 33.123});
  </code></pre>
  
  <p>Now I have to do the same in javascript, but I'm struggling with the regular expression. The following function works fine for the simple placeholders like "{0}" but fails for more complex ones like "{3:0.0}":</p>
  
  <pre><code>function FormatString(txt, args) {
    for (i = 0; i &lt; args.length; i++) {
      txt = txt.replace('{' + i + '}', args[i]);
    }
    return txt;
  }
  
  //e.g:
  FormatString("aa {0} bb {1} cc", [1, 2]);
  // returns "aa 1 bb 2 cc"
  </code></pre>
  
  <p>For the complex placeholders, I tried to modify my function to use regular expressions, but so far I failed to come up with a working RegExp. This is what I tried:</p>
  
  <pre><code>function FormatString2(txt, args) {
    for (i = 0; i &lt; args.length; i++) {
      txt = txt.replace(new RegExp('{' + i + '.*}'), args[i]);
    }
    return txt;
  }
  
  // does not work correctly, i.e:
  FormatString2("aa {0:0.0} bb {1} cc", [1, 2]);
  // returns "aa 1 cc" instead of "aa 1 bb 2 cc"
  </code></pre>
  
  <p>Any hints how to properly replace these .NET-placeholders in javascript (using RegExp or not)?</p>