Javascript Regular Expressions Functionality
<p>I've spent a few hours on this and I can't seem to figure this one out.</p>

<p>In the code below, I'm trying to understand exactly what and how the regular expressions in the <code>url.match</code> are working.</p>

<p>As the code is below, it doesn't work.  However if I remove <code>(?:&amp;toggle=|&amp;ie=utf-8|&amp;FORM=|&amp;aq=|&amp;x=|&amp;gwp)</code> it seems to give me the output that I want.</p>

<p>However, <strong>I don't want to remove this without understanding what it is doing.</strong></p>

<p>I found a pretty useful resource, but after a few hours I still can't precisely determine what these expressions are doing:</p>

<p><a href="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions#Using_Parenthesized_Substring_Matches" rel="nofollow">https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions#Using_Parenthesized_Substring_Matches</a></p>

<p>Could someone break this down for me and explain how exactly it is parsing the strings.  The expressions themselves and the placement of the parentheses is not really clear to me and frankly very confusing.</p>

<p>Any help is appreciated.</p>

<pre><code>(function($) {    

  $(document).ready(function() {         

      function parse_keywords(url){
          var matches = url.match(/.*(?:\?p=|\?q=|&amp;q=|\?s=)([a-zA-Z0-9 +]*)(?:&amp;toggle=|&amp;ie=utf-8|&amp;FORM=|&amp;aq=|&amp;x=|&amp;gwp)/);
          return matches ? matches[1].split('+') : [];

      }
      myRefUrl = "http://www.google.com/url?sa=f&amp;rct=j&amp;url=https://www.mydomain.com/&amp;q=my+keyword+from+google&amp;ei=fUpnUaage8niAKeiICgCA&amp;usg=AFQjCNFAlKg_w5pZzrhwopwgD12c_8z_23Q";

      myk1 = (parse_keywords(myRefUrl));

      kw="";

      for (i=0;i&lt;myk1.length;i++) {
          if (i == (myk1.length - 1)) {
          kw = kw + myk1[i];
          }
          else {
          kw = kw + myk1[i] + '%20';
          }
      }

      console.log (kw);

      if (kw != null &amp;&amp; kw != "" &amp;&amp; kw != " " &amp;&amp; kw != "%20") {

      orighref = $('a#applynlink').attr('href');
      $('a#applynlink').attr('href', orighref + '&amp;scbi=' + kw);
      }                     

  });  

})(jQuery);
</code></pre>
