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')?
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"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments