You need to do something like this:
function replaceQueryString(url,param,value) { var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i"); if (url.match(re)) return url.replace(re,'$1' + param + "=" + value + '$2'); else return url + '&' + param + "=" + value; } $(".hospitalDropDown").change(function(e) { window.location.href = replaceQueryString(window.location.href, 'hos', $(this).val()); )};
The replaceQueryString you found has already been packaged into a function convenient for you, so you do not need to know anything about the regular expression in order to use it. All you have to do is pass this function to your URL, the parameter you are trying to change ("hos"), and the new value.
Also, $(location).attr('href') not really valid, and you didn't use it anyway, so just pull it out and stick to window.location.href .
Finally, although you do not need any knowledge of regular expressions in this particular case, you can certainly benefit from learning at least some basic regular expression syntax. Take a look at http://www.regular-expressions.info/ for some good explanations, and then use a regular expression tester like http://www.regextester.com/ to play around with samples and try to write some of your own.
source share