title: Regular expression to capture Google search term from URL
body: <p>Consider the following URL (which is the result of a Google search in Firefox followed by changing the search terms once via Google's interface which adds the part starting with the <code>#</code>):</p>
  
  <pre><code>https://www.google.de/search?q=initial+search+term&amp;ie=utf-8&amp;oe=utf-8&amp;gws_rd=cr&amp;ei=Zw_rVfjrMMj8abKsn0g#q=changed+search+terms
  </code></pre>
  
  <p>How do I write a RegExp (JavaScript) that captures <code>"changed+search+terms"</code> if present in the URL but `"initial+search+terms" by default?</p>
  
  <p>Also it should be flexible enough to support different TLDs and accept the <code>"q=search+terms"</code> part at different positions in the URL.</p>
  
  <p>So far I have</p>
  
  <pre><code>^https?://([^.]+\.)?google\.([a-z]+\.?)+/[^?/]*\?(.*&amp;)?q=([^&amp;]+)
  </code></pre>
  
  <p>but I can't seem to figure out how to prefer the second <code>"q="</code> if present.</p>