roonie
roonie

Reputation: 803

How to replace query parameter in URL using Regex?

I have to use regular expressions to replace or append a query to a url with the function adjustUrlParameter(url, param). If the same query exists in the URL, it should replace the query with param (i.e. ID=501 should replace ID=200 in google.com?ID=200). If no query exists, it should simply append the query to the end of the url. Finally, if a query does exist but of a different type, it should append the query and separate it from the pre-existing query with a '&'. Here's an example:

adjustUrlParameter('google.com?ID=501', 'TYPE=444') // => 'google.com?ID=501&TYPE=444'

My code isn't replacing the same query type. It is returning 'google.com?ID=501&ID=200' instead of returning 'google.com?ID=200'. Here's my code:

var adjustUrlParameter = function(url, param) {
  var urlQuery = url.match(/\w+\W+/g);
  for (var i = 0; i < urlQuery.length; i++) {
    if (param === urlQuery[i]) {
        return url.replace(/\?.+$/, '?' + param);
    }
  }

  if (url.match(/\?/)) {
    return url + "&" + param;
  } else {
    return url + "?" + param;
  }

};

How do I get adjustUrlParameter('google.com?ID=501', 'ID=201') to return the proper result ('google.com?ID=201')?

Upvotes: 1

Views: 681

Answers (2)

Moishe Lipsker
Moishe Lipsker

Reputation: 3034

You want to compare the names of the queries, not the values they contain too. Say you pass in id=200 as param, you want to find and replace from id in the existing url. That means that we don't care about the value of id. If that is the case, we can split by ? or & to get a list of all of the queries. We start looping at 2 because 0 and 1 are the base url and the ?. We also jump 2 with every iteration to skip the preceding &. Lastly, we make sure to split each query with = and use [0] or the first half to get that name that we care about.

You could do something like this:

var adjustUrlParameter = function(url, param) {
    var urlQuery = url.split(/(\?|\&)/)
    ,   thisQuery = param.split(/\=/)[0]
    for (var i = 2; i < urlQuery.length; i = i + 2) {
        if (thisQuery === urlQuery[i].split(/\=/)[0]) {
            return url.replace(/(\?|\&).*$/, '?' + param);
        }
    }

    if (url.match(/\?/)) {
        return url + "&" + param;
    } else {
        return url + "?" + param;
    }

};

Upvotes: 0

melis
melis

Reputation: 1275

The problem is that the if statement never returns true. Put some console.logs in the for loop and watch it. You'll see when urlQuery[i] returns "ID=" param returns "ID=444". They are not equal.

console.log("parameter:"+param); //parameter:ID=444
console.log("url_query: "+urlQuery[i]); //url_query: ID=

If you change your for loop like below it replaces the query parameter with the same type:

  for (var i = 0; i < urlQuery.length; i++) {
    console.log("parameter:"+param);
    console.log("url_query: "+urlQuery[i]);
    var paramType = param.split("="); 
    console.log("paramType"+paramType); 
    var tmp = paramType[0]+"="; //since urlQuery[i] always has a character at the end
    console.log(tmp);
    if (tmp === urlQuery[i]) {
        return url.replace(/\?.+$/, '?' + param);
    }
  }

When you run adjustUrlParameter('google.com?ID=501', 'ID=444') the output is "google.com?ID=444"

Upvotes: 1

Related Questions