Get query string parameters with Regular Expressions (in JavaScript)!

I’ve said it before, but not quite so explicitly – it’s easiest to get query string parameters with a regular expression. I keep seeing big JavaScript functions looping through and splitting the parameters up, and that might be fine if you want to store those and use them repeatedly – but if not, regexes are your friend.

This function will return the value of a parameter. If the parameter requested isn’t in the query string, it returns null.If the parameter doesn’t have a value (e.g. debug in ...&debug&...) it returns an empty string.

function GetQueryParam(parameter){
var p = escape(unescape(parameter));
var regex = new RegExp("[?&]" + p + "(?:=([^&]*))?","i");
var match = regex.exec(;
var value = null;
if( match != null ){
value = match[1];
return value;

Get query string parameters with Regular Expressions (in JavaScript)!

2 thoughts on “Get query string parameters with Regular Expressions (in JavaScript)!

  1. Panoone says:

    I would dearly love to see this working. I am trying to grab the keyword value from a search result in order to build some dynamic links.

    e.g. k=John

    Try a people search.

  2. latcho says:

    Thanks ! Yes this is an older blog page, but it was there when I needed it on top of google.
    Here the adapted flash AS3 code to obtain a query variable in case someone needs it, as I did 🙂

    public static function browserSearchParam(id:String):String
    if (ExternalInterface.available) {
    var queryString:String =“”);
    var p = id;
    var regex:RegExp = new RegExp(“[?&]” + p + “(?:=([^&]*))?”,”i”);
    var match:Array = regex.exec(queryString);
    var value:String = null;
    if( match && match.length){ value = match[1]; }
    return value;
    return null;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.