I am using jQuery 1.12. I want to replace the query string parameter in the query string of the URL of my window, or add a parameter if it does not exist. I tried below:
new_url = window.location.href.replace( /[\?#].*|$/, "?order_by=" + data_val ) window.location.href = new_url
but I found that it erases all previous parameters in the query string, which I don't need. If the query string:
?a=1&b=2
I would like the new query line to be:
?a=2&b=2&order_by=data
and if the query string was:
?a=2&b=3&order_by=old_data
it will become:
?a=2&b=3&order_by=data
javascript jquery url regex parameters
user7055375
source share