How to replace query parameter in URL using Regex?

roonie

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')?

melis

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to replace query parameter in URL using Regex?

From Dev

How to replace entire URL using Regex?

From Dev

Replace portions of a url path and query string using .htaccess and Regex?

From Dev

How to replace the first parameter name in query string using .htaccess?

From Dev

How to replace a parameter in all of my url's using .htaccess?

From Dev

How to extract an optional query parameter using regex in Javascript

From Dev

How to extract a parameter from a URL using a regex pattern

From Dev

How to verify if a URL has a GET parameter using a regex

From Dev

Using the url query parameter with a controller

From Dev

Using the url query parameter with a controller

From Dev

How to replace query string value in the url using Jquery

From Dev

How would you model bitmask parameter using URL query string?

From Dev

How to replace content using regex

From Dev

Dynamically replace part in URL using Regex

From Dev

Using search and replace on URL query string

From Dev

How can I replace the value of a URL parameter?

From Dev

Append query parameter to start of URL using Retrofit

From Dev

Replace [url=URL] regex

From Dev

RegEx/Javascript: How to get the parameter from URL?

From Dev

How to pass a URL as a query string parameter in MVC

From Dev

how to add query parameter to link url in JQuery?

From Dev

How to pass an array in query parameter url react

From Dev

MySQL Regex Replace Query

From Dev

How to use wildcards with Replace() without using Regex

From Dev

How to replace tokens in java using regex?

From Dev

How to replace surrounding groups of a match using regex?

From Dev

How to replace a specific character in a substring using regex?

From Dev

How to replace exact string using regex? In DrRacket

From Dev

How to replace special characters using regex

Related Related

  1. 1

    How to replace query parameter in URL using Regex?

  2. 2

    How to replace entire URL using Regex?

  3. 3

    Replace portions of a url path and query string using .htaccess and Regex?

  4. 4

    How to replace the first parameter name in query string using .htaccess?

  5. 5

    How to replace a parameter in all of my url's using .htaccess?

  6. 6

    How to extract an optional query parameter using regex in Javascript

  7. 7

    How to extract a parameter from a URL using a regex pattern

  8. 8

    How to verify if a URL has a GET parameter using a regex

  9. 9

    Using the url query parameter with a controller

  10. 10

    Using the url query parameter with a controller

  11. 11

    How to replace query string value in the url using Jquery

  12. 12

    How would you model bitmask parameter using URL query string?

  13. 13

    How to replace content using regex

  14. 14

    Dynamically replace part in URL using Regex

  15. 15

    Using search and replace on URL query string

  16. 16

    How can I replace the value of a URL parameter?

  17. 17

    Append query parameter to start of URL using Retrofit

  18. 18

    Replace [url=URL] regex

  19. 19

    RegEx/Javascript: How to get the parameter from URL?

  20. 20

    How to pass a URL as a query string parameter in MVC

  21. 21

    how to add query parameter to link url in JQuery?

  22. 22

    How to pass an array in query parameter url react

  23. 23

    MySQL Regex Replace Query

  24. 24

    How to use wildcards with Replace() without using Regex

  25. 25

    How to replace tokens in java using regex?

  26. 26

    How to replace surrounding groups of a match using regex?

  27. 27

    How to replace a specific character in a substring using regex?

  28. 28

    How to replace exact string using regex? In DrRacket

  29. 29

    How to replace special characters using regex

HotTag

Archive