<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Comments on snippet: 'Template system (replace items in a string with items from a hash)'</title>
    <description>Snipplr comments feed</description>
    <link>https://snipplr.com/</link>
    <lastBuildDate>Tue, 07 Apr 2026 08:51:42 +0000</lastBuildDate>
    <item>
      <title>mgeduld said on 19/Jan/2011</title>
      <link>https://snipplr.com/view/47454/template-system-replace-items-in-a-string-with-items-from-a-hash</link>
      <description>&lt;p&gt;Incidentally, it pisses me off that I have to run the regular expression twice (lines 5 and 6). Originally, I thought I could do it like this:&#13;
&#13;
var regExp : RegExp = new RegExp( "(%\{(.*?)})","g" ); //NOTE the "g"&#13;
template = template.replace( regExp, replacements[ "$2" ] );&#13;
&#13;
The "g" (global) switch will do all the replacements at once, so no need for a while loop. The $2 refers to the second (inner) parenthesis in the RegExp: (.*?). That will match the word inside the %{  } construct. The 2 in match[ 2 ], above, is doing the same thing. So if the construct is %{ADJECTIVE}, "$2" and match[ 2 ] match ADJECTIVE.&#13;
&#13;
However, what I discovered is that Actionscript won't let you pass the "$2" into another function or use it as a key to a hash. So replacements[ "$2" ] inside a replace() won't get parsed to replacements[ "ADJECTIVE" ].  It will literally look for replacements.$2. &#13;
&#13;
That forced me to first extract ADJECTIVE, with the exec() method, and then to use that string directly in the replace() command. Not a huge deal to run a RegExp twice, but not ideal in terms of optimization.&#13;
&#13;
Darn.&#13;
&#13;
If anyone knows a way around this, let me know.&lt;/p&gt;</description>
      <pubDate>Wed, 19 Jan 2011 07:36:26 UTC</pubDate>
      <guid>https://snipplr.com/view/47454/template-system-replace-items-in-a-string-with-items-from-a-hash</guid>
    </item>
    <item>
      <title>patsm00re18 said on 30/Mar/2021</title>
      <link>https://snipplr.com/view/47454/template-system-replace-items-in-a-string-with-items-from-a-hash</link>
      <description>&lt;p&gt;Good to know that the previous string has been fixed now! &lt;a href="https://www.google.com"&gt;Link to Google&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 30 Mar 2021 02:36:34 UTC</pubDate>
      <guid>https://snipplr.com/view/47454/template-system-replace-items-in-a-string-with-items-from-a-hash</guid>
    </item>
    <item>
      <title>AlyssaLauren8293 said on 03/Oct/2022</title>
      <link>https://snipplr.com/view/47454/template-system-replace-items-in-a-string-with-items-from-a-hash</link>
      <description>&lt;p&gt;It was so nice to see this snipper code you shared here. The above code shared here belongs &lt;a href="https://peterveresmd.ca/"&gt;peter veres breast cancer treatment&lt;/a&gt; to the template system that replaces items in a string with items from a hash. I am looking here for more such updates regarding that and keep sharing more details over here. Keep sharing more updates here.&lt;/p&gt;</description>
      <pubDate>Mon, 03 Oct 2022 07:43:18 UTC</pubDate>
      <guid>https://snipplr.com/view/47454/template-system-replace-items-in-a-string-with-items-from-a-hash</guid>
    </item>
  </channel>
</rss>
