title: Why doesn't the javascript replace global flag work in Chrome or IE, and how to I work around it?
body: <p>According to the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace#Example.3a_Using_global_and_ignore_with_replace" rel="noreferrer"><code>String.prototype.replace()</code> page on MDN</a>, I should be able to easily replace multiple patterns just by using</p>
  
  <pre><code>str.replace('what to replace', 'replace with', 'flags')
  </code></pre>
  
  <p>and setting the flags to <code>'g'</code>. </p>
  
  <p>It works perfect in Firefox 3.6. But in Chrome and IE8, it only replaces the first <code>'what to replace'</code>. </p>
  
  <p>I can use the</p>
  
  <pre><code>str.replace(/what to replace/gi, 'replace with')
  </code></pre>
  
  <p>syntax. But I'm pulling the <code>'what to replace'</code> out of an array, which makes it hard to add the flags in that syntax.</p>
  
  <p>Here's the code I'm trying to use. How to I modify it to work in Chrome as well as Firefox?</p>
  
  <pre><code>function generateQuestion()
  {
      //alert('variable length: '+variableList.length);
      for(i=0;i&lt;variableList.length;i++)
      {
          variable = variableList[i];
          rep = replacementList[i];
          flags = "gi";
          questionText = questionText.replace(variable, rep, flags);
      }
  }
  </code></pre>
  
  <p>And why do I have to bother modifying it at all? Shouldn't Chrome evaluate the JavaScript as described in the link?</p>