title: Javascript: Set cookie - Dynamic URL parameter Name
body: <p>I've been strugling looking for a way to set a cookie if a URL parameter is persent. The problem is, the name of the URL parameter is half dynamic.</p>
  
  <p>The URL would be: </p>
  
  <pre><code>http://zzzz.test.bbbb/index.html?transaction[XXXX]=YYYYY
  </code></pre>
  
  <p>The XXXX is the userID that will change depending on the customer and YYYY is the actual value (transaction number).</p>
  
  <p>I have the following code but it won't recognize the brakets nor what is inside. How can I set it to recognize the URL parameter including the brakets and the dynamic userID within the brakets?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>function URLparam(name) {
      return unescape(
          (RegExp(name + '=' + '(.+?)(&amp;|$)').exec(location.search)||[,null])[1]
      );
  }
  
  if (URLparam("transaction") !== null &amp;&amp; !get_cookie ("Transacion_ID")) {
      set_cookie ("Transacion_ID", URLparam("transaction"));
  }
  
  if (get_cookie ("Transacion_ID")) {
      console.log(get_cookie ("transaction"));
  }
  });
  </code></pre>
  
  <p>Any hint would be much appreciated.</p>
  
  <p>Thanks!</p>
  
  <p>NEW ADDITION ==========================</p>
  
  <p>For a link with the following format: </p>
  
  <pre><code>http://zzzz.test.bbbb/index.html?transaction[XXXX][zzzzz]=YYYYY
  </code></pre>
  
  <p>How would the correct <code>getParameterByName</code> function look like in order to recognize the URL parameter <code>transaction[XXXX][zzzzz]</code> ?</p>
  
  <p>I've tried this but it does not work:</p>
  
  <pre><code>function getParameterByName(name) {
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regex = new RegExp(name + '(?:\\[\\d+\\]?:\\[\\d+\\])?=' + '(.+?)(&amp;|$)'),
      results = regex.exec(location.search);
  return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  }
  </code></pre>
  
  <p>Any ideas?</p>