In C#, I'm trying to use regular expressions to replace values in a querystring. So, if I have:
http://www.url.com/page.aspx?id=1
I'd like to write a function where I pass in the url, the querystring value and the value to replace. Something along the lines of:
string url = "http://www.url.com/page.aspx?id=1";
string newURL = ReplaceQueryStringValue(url, "id", "2");
private string ReplaceQueryStringValue(string url, string replaceWhat, string replaceWith)
{
return Regex.Replace(url, "[^?]+(?:\?"+replaceWhat+"=([^&]+).*)?",replaceWith);
}