1

Using a regular expression, I want to write a function that will take a URL and a parameter name: ReplaceParamValueinURL (url, param, value).

If the parameter exists, it will replace the value in the URL.
If the parameter doesn't exist, it will add it to the URL along with the value.
If the parameter exists with no value, it will add the value to the parameter.

Is there an elegant way of doing all three in regex find and replace?

ReplaceParamValueinURL ("http://google.com?a=1&b=2&c=3, a , 4)
returns http://google.com?a=4&b=2&c=3 

ReplaceParamValueinURL ("http://google.com?a=1&b=2&c=3, a , 4)
returns http://google.com?a=4&b=2&c=3 

ReplaceParamValueinURL ("http://google.com?a=1&b=2&c=3, c , 4)
returns http://google.com?a=1&b=2&c=4 

ReplaceParamValueinURL ("http://google.com?a=1&b=2&c=3, d , 5)
returns http://google.com?a=1&b=2&c=3&d=5 

ReplaceParamValueinURL ("http://google.com?aaa=0&a=1&b=2&c=3, a , 6)
returns http://google.com?aaa=0&a=6&b=2&c=3 


ReplaceParamValueinURL ("http://google.com?a=1&b&c=3, b , 2)
returns http://google.com?a=1&b=2&c=3 

I am hoping to do this with Reg ex instead of split. I really appreciate it if you can explain your answer if the regex is too complex. Is there a jQuery function that already does this? 

I guess it's a very common case but can have many corner cases.

ReplaceParamValueinURL ("http://google.com?a=1&b&c=3#test, a , 2)
returns http://google.com?a=2&b&c=3#test

2 Answers 2

3

No you can't do it with a single regexp, but the function is pretty simple, i've tested it with all your examples so it should work:

function ReplaceParamValueinURL (url, name, val) {

    //Try to replace the parameter if it's present in the url
    var count = 0;
    url = url.replace(new RegExp("([\\?&]" + name + "=)[^&]+"), function (a, match) {
        count = 1;
        return match + val;
    });

    //If The parameter is not present in the url append it
    if (!count) {
        url += (url.indexOf("?") >=0 ? "&" : "?") + name + "=" + val;
    }

    return url;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I just realized this solution does not work with my real url localhost:8080/…
0

try this,

function ReplaceParamValueinURL(url , replceparam , replaceValue)
 {
  regExpression = "(\\?|&)"+replceparam+"(=).(&|)";
  var regExpS = new RegExp(regExpression, "gm");
  var getmatch = url.match(regExpS);
  var regExpSEq = new RegExp("=", "g");
  var getEqalpostion = regExpSEq.exec(getmatch);
  var newValue;
  if(getmatch[0].charAt(getmatch[0].length - 1) != "&")
    {
      var subSrtingToReplace  = getmatch[0].substring((getEqalpostion.index+ 1),getmatch[0].length );
      newValue =  getmatch[0].replace(subSrtingToReplace , replaceValue);
    }
  else
   {
      var subSrtingToReplace  = getmatch[0].substring((getEqalpostion.index+ 1) , getmatch[0].length - 1 );
      newValue =  getmatch[0].replace(subSrtingToReplace , replaceValue);
   }

 return  returnString = url.replace(regExpS , newValue);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.