I am trying to do a js regex that will search for a parameter in an URL and if found replace it's value. If the parameter is not found, then it will be added to the URL.
Here is a couple of scenarios
http://www.domain.com/?paramZ=123456
https://www.domain.com/?param1=val1;param2=val2;param3=val3;paramZ=123456
http://www.domain.com/one/or/more?paramZ=123456;param1=val1;param2=val2;param3=val3
https://www.domain.com/one/or/more?param1=val1;paramZ=123456;param2=val2;param3=val3
http://www.domain.com/one/or/more?param1=val1;param2=val2;param3=val3
My goal is to do the following:
- find paramZ=XXXXXX and replace it with paramZ=YYYYYY
- if paramZ does not exist, add it with the value YYYYYY
I've managed to do the first one:
search for
/^(http[s]?:\/\/www\.domain)(.*)(paramZ\=[\d]+)(.*)$/g
replace with
$1$2paramZ=987654$4
Here is a working example: https://regex101.com/r/9Ui8vx/1
I don't know even if it's possible to acheive this in only one regex.