title: JavaScript Regular Expression to Split an Ampersand Delimited String
body: <p>I've been at this for hours, and I'm hitting a dead end. I've read up on Regular Expressions all over the place, but I'm still having trouble matching on anything more complex than basic patterns.</p>
  
  <p>So, my problem is this: </p>
  
  <p>I need to split an "&amp;" delimited to string into a list of objects, but I need to account for the values containing the ampersand as well.</p>
  
  <p>Please let me know if you can provide any help.</p>
  
  <p><code>var subjectA = 'myTestKey=this is my test data &amp; such&amp;myOtherKey=this is the other value';</code></p>
  
  <p><strong>Update:</strong></p>
  
  <p>Alright, to begin with, thanks for the awesome, thoughtful responses. To give a little background on why I'm doing this, it's to create a cookie utility in JavaScript that is a little more intelligent and supports keys ala ASP. </p>
  
  <p>With that being said, I'm finding that the following RegExp <code>/([^&amp;=\s]+)=(([^&amp;]*)(&amp;[^&amp;=\s]*)*)(&amp;|$)/g</code> does 99% of what I need it to. I changed the RegExp suggested by the contributors below to also ignore empty spaces. This allowed me to turn the string above into the following collection:</p>
  
  <pre><code>[
      [myTestKey, this is my test data &amp; such],
      [myOtherKey, this is the other value]]
  ]
  </code></pre>
  
  <p>It even works in some more extreme examples, allowing me to turn a string like:</p>
  
  <p><code>var subjectB = 'thisstuff===myv=alue me==&amp; other things=&amp;thatstuff=my other value too';</code></p>
  
  <p>Into:</p>
  
  <pre><code>[
      [thisstuff, ==myv=alue me==&amp; other things=],
      [thatstuff, my other value too]
  ]
  </code></pre>
  
  <p>However, when you take a string like: </p>
  
  <p><code>var subjectC = 'me===regexs are hard for &amp;me&amp;you=&amp;you=nah, not really you\'re just a n00b';</code></p>
  
  <p>Everything gets out of whack again. I understand why this is happening as a result of the regular expression above (kudos for a very awesome explanation), but I'm (obviously) not comfortable enough with regular expressions to figure out a work around.</p>
  
  <p>As far as importance goes, I need this cookie utility to be able to read and write cookies that can be understood by ASP and ASP.NET &amp; vice versa. From playing with the example above, I'm thinking that we've taken it as far as we can, but if I'm wrong, any additional input would be greatly appreciated.</p>
  
  <p>tl;dr - Almost there, but is it possible to account for outliers like <code>subjectC</code>? </p>
  
  <p><code>var subjectC = 'me===regexs are hard for &amp;me&amp;you=&amp;you=nah, not really you\'re just a n00b';</code></p>
  
  <p>Actual ouput:</p>
  
  <pre><code>[
      [me, ==regexs are hard for &amp;me],
      [you, ],
      [you, nah, not really you\'re just a n00b]
  ]
  </code></pre>
  
  <p>Versus expected output:</p>
  
  <pre><code>[
      [me, ==regexs are hard for &amp;me&amp;you=],
      [you, nah, not really you\'re just a n00b]
  ]
  </code></pre>
  
  <p>Thanks again for all of your help. Also, I'm actually getting better with RegExp... Crazy.</p>