I'm trying to remove a variable from a query string. This is my current approach:
var str = "http://example.com/tal.php?sec=16&name=xxxx&size=xxxx";
str = str.replace(/([&])name=.*?(&|$)/g,"");
There are two problems with this code:
Both the
&at the beginning of name and at the end get removed. I'd like only one to be removedIt should also work when the variable name is at the beginning or at the end of the query string.
How can I tell the regex to delete the second & only if it has matched the first one?