I am trying to replace some value in the query string of the current page using JS. Eg: from category=Old Value
to category=New Value
.
To code looks like this:
var sNewValue = 'New Value';
sQueryString = sQueryString.replace(/category=[^&]+&?/, 'category=' + sNewValue);
It works fine except for values that have ampersands. Luckily all the ampersands are URL encoded, so I know I should discard the amp; before the &. I think I should use lookaheads, but I don't know how. I tried this regular expression, but with no luck: /category=[^&(?!amp;)]+&?/
Thanks
You don’t need to specify a following
&
at all:Because
[^&]
does already match any character except&
. Furthermore, if category is the last parameter, there even is no following&
.Why are ampersands being encoded as & instead of %26? Or am I reading your question wrong?
If that's the way it needs to be, it might be easier for you to deal with this query string if you break it into name/value pairs first.
Then you can work with each value without worrying if it contains an encoded & or not.
You don't get lookbehind in JavaScript regular expressions. You do get lookahead, but it is unreliable in IE, so best avoided unless you really know what you're doing.
yeah, this doesn't really make any sense. You can't use lookahead in a
[]
character group: what you're saying here is match characters that aren't&
,(
,?
,!
,a
,m
,p
,;
, or)
.However you should not see
&
anyway: you should be working on a plain, unencoded query string, eg. as fetched fromlocation.search
. (If you are hacking at HTML markup in a string with regex you've got much, much bigger problems.)If you are getting the query string from
location.search
, you'll have a?
on the front if there's any query. So you can match the beginning with either&
or?
, and do your regex match on:note I have also included
;
as a possible separator as per HTML4 section B.2.2, and usedencodeURIComponent
so that invalid characters like spaces and special characters like&
itself get properly URL-encoded. Also the character$
, which otherwise has special meaning in a regexp replacement string.This is still quite messy, and doesn't cope with URL-encoded parameter names (ie
c%61tegory
is a valid alternative way of sayingcategory
) or multiple parameters with the same name. If you want to be more robust about it you could dump the regexps and do full query string parsing/reconstruction. See the functions from this answer and do:You can try:
This will replace even if category is preceded by
?
as in/foo.php?category=bar
OK, I think I actually found what you need.
/(\bcategory=)(([^&]|&\;)*)/
This includes the many different scenarios:
You may want to add a global switch.