Find-and-replace optimization I'm looking for some JavaScript code improvement and optimization. Is it possible to optimize this block of code (for example, remove double array loop)?

replace: function(ex, str) {
    var arr = str.match(/{add_([A-Z]+)}/gi);
    if (arr &amp;&amp; arr.length) {
       arr.each(function(e){            
          var id = e.replace('{add_','').replace('}',''),
          o = Core.get_data_by_id(ex.extensions, id);   

          if (o &amp;&amp; o.status =="1") {
            str = str.replace('{add_'+id+'}', o.html);
          } 
       });
    } 

    var arr2 = str.match(/{banner_([0-9]+)}/gi);
    if (arr2 &amp;&amp; arr2.length) {
       arr2.each(function(e){
         var id = e.replace('{banner_','').replace('}',''),
         b = Core.get_data_by_id(ex.banners, id);

         if (b &amp;&amp; b.status =="1" &amp;&amp; b.code &amp;&amp; b.code.length) {
            var content = '&lt;div class="units-row banner_'+b.id+'"&gt;&lt;ul&gt;' +
            b.code.each(function(s){
               content += '&lt;li&gt;'+s.code+'&lt;/li&gt;';
            });                     
            content += '&lt;/ul&gt;&lt;/div&gt;';
            str = str.replace('{banner_'+id+'}', content);
         } 
       });
    }

    return str;
}
You should be using String.prototype.replace() with a function as the second argument. Example:

str = str.replace(/{add_([A-Z]+)}/gi, function(match, id) {
  var o = Core.get_data_by_id(ex.extensions, id);   
  return (o &amp;&amp; o.status == "1") ? o.html : match;
}); 

str = str.replace(/{banner_([0-9]+)}/gi, function(match, id){
  var b = Core.get_data_by_id(ex.banners, id);

  if (b &amp;&amp; b.status =="1" &amp;&amp; b.code &amp;&amp; b.code.length) {
    var content = '&lt;div class="units-row banner_'+b.id+'"&gt;&lt;ul&gt;' +
    b.code.each(function(s){
       content += '&lt;li&gt;'+s.code+'&lt;/li&gt;';
    });                     
    content += '&lt;/ul&gt;&lt;/div&gt;';
    return content;
  } 
  return match; 
});


EDIT: I've added the second part.