wwarby
asked on
Replace Specified Part of QueryString With JavaScript
I need a function which will replace a given part of a querystring. The function needs to look like this:
function replaceQueryStringPart(url , name, value) {
...
}
It needs to work as follows:
replaceQueryStringPart('http://localhost/test.asp?name=brian&phone=12345', 'name', 'fred') //Returns http://localhost/test.asp?name=fred&phone=12345
replaceQueryStringPart('http://localhost/test.asp?name=brian&phone=12345', 'phone', '67890') //Returns http://localhost/test.asp?name=brian&phone=67890
If the specified querystring part does not exist, it must be appended to the end of the querystring as follows:
replaceQueryStringPart('http://localhost/test.asp?name=brian&phone=12345', 'email', 'test@server.com') //Returns http://localhost/test.asp?name=brian&phone=67890&email=test@server.com
replaceQueryStringPart('http://localhost/test.asp', 'email', 'test@server.com') //Returns http://localhost/test.asp?email=test@server.com
I have spent a few hours on this now and realised I need to use the JavaScript RegExp object with the replace method, but I can't get my head around regular expressions enough to write the function. Would be very grateful for any assistance.
-William
function replaceQueryStringPart(url
...
}
It needs to work as follows:
replaceQueryStringPart('http://localhost/test.asp?name=brian&phone=12345', 'name', 'fred') //Returns http://localhost/test.asp?name=fred&phone=12345
replaceQueryStringPart('http://localhost/test.asp?name=brian&phone=12345', 'phone', '67890') //Returns http://localhost/test.asp?name=brian&phone=67890
If the specified querystring part does not exist, it must be appended to the end of the querystring as follows:
replaceQueryStringPart('http://localhost/test.asp?name=brian&phone=12345', 'email', 'test@server.com') //Returns http://localhost/test.asp?name=brian&phone=67890&email=test@server.com
replaceQueryStringPart('http://localhost/test.asp', 'email', 'test@server.com') //Returns http://localhost/test.asp?email=test@server.com
I have spent a few hours on this now and realised I need to use the JavaScript RegExp object with the replace method, but I can't get my head around regular expressions enough to write the function. Would be very grateful for any assistance.
-William
ASKER
It's almost there, except the first alert box returns 'http://localhost/test.asp&phone=12345?name=fred', which isn't correct syntax. I'll have a look at what you've done and see if I can fix it myself - if you have any suggestions I'd be really grateful. Other than that, it's exactly what I wanted. These regular expressions seem like an impenetrable encrypted language to me - I've read three tutorials and am no closer to understanding them.
-William
-William
Uhps sorry. I did not test it well.
Here the revised version:
<script>
function replaceQueryStringPart(the Url, theParam, theValue){
theUrl = theUrl.replace(new RegExp("\\&?"+theParam+"\\ =[^\\&]+", "i"),"");
theUrl = theUrl.replace("?&","?");
if(theUrl.indexOf("?")>-1) {
return theUrl+"&"+theParam+"="+th eValue
} else {
return theUrl+"?"+theParam+"="+th eValue
}
}
alert(replaceQueryStringPa rt('http://localhost/test.asp?name=brian&phone=12345', 'name', 'fred'));
alert(replaceQueryStringPa rt('http://localhost/test.asp?name=brian&phone=12345', 'phone', '67890'));
alert(replaceQueryStringPa rt('http://localhost/test.asp?name=brian&phone=12345', 'email', 'test@server.com'));
alert(replaceQueryStringPa rt('http://localhost/test.asp', 'email', 'test@server.com'));
</script>
The problem with upper RegExp is that it has to be expressed as string and that is additionaly confusing because of doubled back slashes.
The basic expression is this:
String.replace(/\&?parm\=[ ^\&]+/, "")
And that expression matches this:
\&? : zero or one & character
param\= : matches that string "param="
[^\&]+ : matches all characters until the next & character.
So the matched upper characters are replaced by empty string (they are removed from the URL)
The additional String.replace("?@", "?") does only a paramter shift is the removed paramter was the first parameter (Uhps now I see next problem and will be back in a second...)
Here the revised version:
<script>
function replaceQueryStringPart(the
theUrl = theUrl.replace(new RegExp("\\&?"+theParam+"\\
theUrl = theUrl.replace("?&","?");
if(theUrl.indexOf("?")>-1)
return theUrl+"&"+theParam+"="+th
} else {
return theUrl+"?"+theParam+"="+th
}
}
alert(replaceQueryStringPa
alert(replaceQueryStringPa
alert(replaceQueryStringPa
alert(replaceQueryStringPa
</script>
The problem with upper RegExp is that it has to be expressed as string and that is additionaly confusing because of doubled back slashes.
The basic expression is this:
String.replace(/\&?parm\=[
And that expression matches this:
\&? : zero or one & character
param\= : matches that string "param="
[^\&]+ : matches all characters until the next & character.
So the matched upper characters are replaced by empty string (they are removed from the URL)
The additional String.replace("?@", "?") does only a paramter shift is the removed paramter was the first parameter (Uhps now I see next problem and will be back in a second...)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks very much ;-)
Your last one seems to work fine, although I think I may have cracked it myself from your first reply - here's my solution:
<script>
function replaceQueryStringPart(the Url, theParam, theValue){
if (theUrl.indexOf('?' + theParam)>-1) {
theUrl = theUrl.replace(new RegExp('\\??'+theParam+'\\ =[^\\&]+', 'gi'),'?'+ theParam+' ='+theValu e);
} else if (theUrl.indexOf('&' + theParam)>-1) {
theUrl = theUrl.replace(new RegExp('\\&?'+theParam+'\\ =[^\\&]+', 'gi'),'&'+ theParam+' ='+theValu e);
} else {
if (theUrl.indexOf("?")>-1) {
return theUrl+"&"+theParam+"="+th eValue
} else {
return theUrl+"?"+theParam+"="+th eValue
}
}
return theUrl
}
alert(replaceQueryStringPa rt('http://localhost/test.asp?name=brian&phone=12345&email=test@server.com', 'name', 'fred'));
alert(replaceQueryStringPa rt('http://localhost/test.asp?name=brian&phone=12345&email=test@server.com', 'phone', '67890'));
alert(replaceQueryStringPa rt('http://localhost/test.asp?name=brian&phone=12345&email=test@server.com', 'email', 'foo@bar.com'));
alert(replaceQueryStringPa rt('http://localhost/test.asp', 'fax', '34567'));
</script>
Certainly couldn't have done it without your example ;-)
-William
Your last one seems to work fine, although I think I may have cracked it myself from your first reply - here's my solution:
<script>
function replaceQueryStringPart(the
if (theUrl.indexOf('?' + theParam)>-1) {
theUrl = theUrl.replace(new RegExp('\\??'+theParam+'\\
} else if (theUrl.indexOf('&' + theParam)>-1) {
theUrl = theUrl.replace(new RegExp('\\&?'+theParam+'\\
} else {
if (theUrl.indexOf("?")>-1) {
return theUrl+"&"+theParam+"="+th
} else {
return theUrl+"?"+theParam+"="+th
}
}
return theUrl
}
alert(replaceQueryStringPa
alert(replaceQueryStringPa
alert(replaceQueryStringPa
alert(replaceQueryStringPa
</script>
Certainly couldn't have done it without your example ;-)
-William
Thanks for points.
And you are right, go for your solution. That is the only real way!
See you,
Zvonko
And you are right, go for your solution. That is the only real way!
See you,
Zvonko
<script>
function replaceQueryStringPart(the
theUrl = theUrl.replace(new RegExp("[\\?\\&]?"+thePara
if(theUrl.indexOf("?")>-1)
return theUrl+"&"+theParam+"="+th
} else {
return theUrl+"?"+theParam+"="+th
}
}
alert(replaceQueryStringPa
alert(replaceQueryStringPa
alert(replaceQueryStringPa
alert(replaceQueryStringPa
</script>