I’m having trouble replacing some url variables in javascript. I’m trying to replace the value of &s= and &so=. Below is my code:
url = url.replace(/(s=).*?(&)/, '$1' + $("#sort_by").val() + '$2');
url = url.replace(/(so=).*?(&)/, '$1' + $("#sort_ord").val() + '$2');
The first replace works no problem, the s value is always replaced, but the so variable never seems to get replaced.
Change your code to this:
This should work with most URL (but not all URL as allowed by the RFC). This takes advantage of the fact that
&is mostly used to delimit key-value pair in the query string of the URL.