title: Replacement specific identifer with regex
body: <p>I have a java regex for replacing all instances of a specific identifier in a script.</p>
  
  <p>This is the search regex that searchers for the "foo" identifier:</p>
  
  <pre><code>([^\w_]|^)foo([^\w\d_]|$)
  </code></pre>
  
  <p>And this is the replacement:</p>
  
  <pre><code>$1bar$2
  </code></pre>
  
  <p>Doing a replaceAll in something like</p>
  
  <pre><code>for foo: [1,2,3];foo&amp;&amp;foo;
  </code></pre>
  
  <p>works well, it outputs</p>
  
  <pre><code>for bar: [1,2,3];bar&amp;&amp;bar;
  </code></pre>
  
  <p>However, when we apply this to a string with two instances of the identifier separated by a single character, it only replaces the first:</p>
  
  <pre><code>foo&amp;foo
  </code></pre>
  
  <p>outputs</p>
  
  <pre><code>bar&amp;foo
  </code></pre>
  
  <p>This happens, I think, because the first match is "bar&amp;" and so when analyzing the rest of the string no other match is found.</p>
  
  <p>Is there a way to fix this by changing the regex only?</p>