Using URL Parameters

Using URL Parameters

I am using some URL parameters in my jQuery and have come across a bug. Since JS is not my primary (or even secondary if I'm honest) language I am at a loss to know why.

I have the following function.
  1. function urlParam(name){
  2. var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
  3. if (results == null) {
  4. return null;
  5. } else {
  6. return results[1] || 0;
  7. }
  8. };
I then call this with the following lines (much reduced for debugging)...
  1. var urlProduct = urlParam('product');
  2. if (urlProduct != null) {
  3. alert(urlProduct);
  4. };
if I go to http://mysite/dir/?product=xxx then all seems to work unless the string xxx contains a lower-case a, m or p. any of these 3 letters will interrupt the string so...

"?product=123a456" will be "123"
but
"?product=123b456" will correctly return "123b456"

As far as I can tell it is only these 3 letters, upper-case works fine so 123A456 will work. It also make no diference where in the string they occur.

I assume the issue is being caused by some faulty regex, specifically in this bit...
  1. '=([^&#]*)'
but can someone with more knowledge than help me fix it?

Thanks.