Capture value out of query string with regex?

/name=([^&]*)/ remove the ^ and end with an & Example: var str = “/pages/new?name=J&return_url=/page/new”; var matches = str.match(/name=([^&]*)/); alert(matches[1]); The better way is to break all the params down (Example using current address): function getParams (str) { var queryString = str || window.location.search || ”; var keyValPairs = []; var params = {}; queryString = … Read more

How to efficiently remove a query string by Key from a Url?

This works well: public static string RemoveQueryStringByKey(string url, string key) { var uri = new Uri(url); // this gets all the query string key value pairs as a collection var newQueryString = HttpUtility.ParseQueryString(uri.Query); // this removes the key if exists newQueryString.Remove(key); // this gets the page path from root without QueryString string pagePathWithoutQueryString = uri.GetLeftPart(UriPartial.Path); … Read more

How to update querystring in C#?

To modify an existing QueryString value use this approach: var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString()); nameValues.Set(“sortBy”, “4”); string url = Request.Url.AbsolutePath; Response.Redirect(url + “?” + nameValues); // ToString() is called implicitly I go into more detail in another response.

How can I match query string variables with mod_rewrite?

RewriteCond %{QUERY_STRING} book=(\w+)&page=(\d+) RewriteRule ^index.php /%1/%2? [L,R=301] Because RewriteRule only looks at the path (up to but not including the question mark), use RewriteCond to capture the values in the query string. Note that the matches from RewriteCond are captured in %1, %2, etc., rather than $1, $2, etc. Also note the ? at the … Read more

Why is “&reg” being rendered as “®” without the bounding semicolon

Although valid character references always have a semicolon at the end, some invalid named character references without a semicolon are, for backward compatibility reasons, recognized by modern browsers’ HTML parsers. Either you know what that entire list is, or you follow the HTML5 rules for when & is valid without being escaped (e.g. when followed … Read more

Strip off specific parameter from URL’s querystring

The safest “correct” method would be: Parse the url into an array with parse_url() Extract the query portion, decompose that into an array using parse_str() Delete the query parameters you want by unset() them from the array Rebuild the original url using http_build_query() Quick and dirty is to use a string search/replace and/or regex to … Read more