-2

Possible Duplicate:
Get query string values in JavaScript

I "had" this pattern but it didn't quite give me what I need, though it worked.

var regex = new RegExp('\\b' + data.keyword + '(=[^&]*)?(&|$)',"gi");

But the following regular expressions "is" what I need but i can't seem to get it to work within the regex obj: /&dyanmicTxtHere(\=[^&])?(?=&|$)|^dyanmicTxtHere(\=[^&])?(&|$)/

I tried: This is NOT working -

var regex = new RegExp('&' + data.keyword + '(=[^&]*)?|^' + data.keyword + '(=[^&]*)?&?',"gi");

I can't figure out why. So the above regex should strip out my passed param (and vlaue)(data.keyword), and deal with the ? and & wherever that param sits in the url. It

So, what would this match?

www.someurl.com?Keyword=foo

www.someurl.com?up=down&Keyword=foo

www.somurl.com?up=down&keyword=foo&left=right

etc... so, if I passed in "Keyword" as my dynamic param, then it would remove it and its associated value.

Community
  • 1
  • 1
james emanon
  • 11,185
  • 11
  • 56
  • 97

3 Answers3

3

Looks to me that what you want is to read parameter values from a request, via JavaScript, correct?

Try using this:

function getParameterByName(name)
{
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.search);
    if(results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

Taken from this question.

Community
  • 1
  • 1
Apoc
  • 88
  • 1
  • 6
  • I don't really want to "read" the params, I want to be able to delete one and its value. The one I want to delete could be dynamically decided. So, I pass in my paramToDelete and my regex (posted above) will strip it and its value. – james emanon Aug 28 '12 at 18:13
  • So you could easily rebuild the query string by reading all parameters minus the one you want to exclude. In the question I linked, there are other functions that retrieve all parameters at once. – Apoc Aug 28 '12 at 18:17
1

If you need to see what the produced regex looks like, simply call regex.toString()

If you do that on the one you tried you get:

/&dyanmicTxtHere(=[^&]*)?|^dyanmicTxtHere(=[^&]*)?&?/gi

Based on that, you can fairly see what needs changing to make it like the regex you provided:

var regex = new RegExp('&' + data.keyword + '(\\=[^&])?(?=&|$)|^' + data.keyword + '(\\=[^&])?(&|$)', '');

If we call toString on that we get:

/&dyanmicTxtHere(\=[^&])?(?=&|$)|^dyanmicTxtHere(\=[^&])?(&|$)/

If that doesn't work, try explaining exactly what you're trying to parse.

ForbesLindesay
  • 10,482
  • 3
  • 47
  • 74
1

Obviously what follows is not a regular expression; but it should, I think, fulfil your requirements. It takes advantage of the requirements of name-value pairs in a GET query-string, and uses simple string, and some array, manipulation to examine the various (if any) name-value pairs from the passed URL:

function removeParam(url, param) {
    // checks for the existence of a '?' in the passed url:
    var queryAt = url.indexOf('?') + 1;
    /* function exits if:
       1. no 'url' parameter was passed,
       2. no 'param' parameter was passed in, or
       3. the 'queryAt' variable is false, or has a falsey value */
    if ((!url || !param) || !queryAt) {
        return false;
    }
    else {
        /* if the 'param' passed in wasn't found in the 'url' variable,
           the function exits, returning the original url (as no modification
           is required */
        if (url.indexOf(param) == -1) {
            return url;
        }
        else {
                /* gets the substring of the url from the first
                   character *after* the '?' */
            var all = url.substring(queryAt),
                // creates an array of the name and value pairs
                nameValues = all.split('&'),
                // creating a new array
                newNameValues = [],
                parts;
            // iterates through each name-value pair
            for (var i = 0, len = nameValues.length; i < len; i++) {
                // splits the name-value pair into two parts
                parts = nameValues[i].split('=');
                /* if the first part (the 'name' of the param) does not
                   matches what you're looking for then the whole name-value
                   pair is pushed into the 'newNameValues' array */
                if (parts[0] !== param) {
                    newNameValues.push(nameValues[i]);
                }
            }
            // returns the reconstructed URL
            return url.substring(0, queryAt) + newNameValues.join('&');
        }
    }
}

JS Fiddle demo.

David Thomas
  • 249,100
  • 51
  • 377
  • 410