title: How might I perform conditional replacement using JavaScript's RegExp?
body: <p>I am trying to wrap my head around using regular expressions and replace() with JavaScript, but have not yet been successful</p>
  
  <p>Suppose I have a string containing this:</p>
  
  <pre><code>&lt;img alt="a picture of ..." src="image/1533?foo=1&amp;bar=2$zot=3" width="500" /&gt;
  </code></pre>
  
  <p>If <code>zot=3</code> I want remove foo (and its value) (or replace <code>foo=x</code> with an empty string).</p>
  
  <p>The replacement would look like this:</p>
  
  <pre><code>&lt;img alt="a picture of ..." src="picture/1533?bar=2$zot=3" width="500" /&gt;
  </code></pre>
  
  <p>I want it to be as bullet-proof as possible as I can never be sure which order the URL parameters will be given in.</p>
  
  <p>Is this possible using a single regex, or are there better solutions?</p>
  
  <p>I was thinking of using DOM and </p>
  
  <ul>
  <li>traversing all <code>img</code> nodes</li>
  <li>get the <code>src</code> attribute</li>
  <li>do a test if <code>@src</code> value has <code>zot=3</code></li>
  <li>if it has <code>zot=3</code>, replace <code>foo=1</code> with an empty string</li>
  </ul>
  
  <p>Of course I have to make sure that any ampersand and so on is removed too.</p>
  
  <p>But I hope to resolve it using a regex or two,</p>
  
  <p>Thanks for any and all answers and advice!</p>