Get query parameter from the current URL with Javascript

When working with Javascript, you sometimes want to fetch the value of a query parameter from the current URL. I found multiple jQuery plugins to do this (in the horrible tradition of writing jQuery plugins for the most miniscule and simplest of tasks) but I ended up writing it myself in seven lines of Coffeescript (and I'm sure it could be compressed to one or two).

Update: as pointed out in the comments, my method has the flaw that it doesn't escape the query parameter it looks for, so it certain circumstances it might match the wrong parameter. If that concerns you, I would recommend to use this alternative method.

Check it out:

getQueryParam = (param) ->
  regex = new RegExp("[?&]#{encodeURIComponent(param)}=([^&]*)")
  match = regex.exec(
  if match?

Discuss this post on Hacker News

Ideas? Constructive criticism? Think I'm stupid? Let me know in the comments!