Reputation:
I'm trying to use .replace() to replace a specific part of a URL but I'm not sure how to go about it.
Specifically, I'm taking a string that looks like this:
'param1=www.DOMAIN.NAME¶m2=NUM1¶m3=true¶m4=25'
I'd like to change param2 at my own discretion using jquery.
Currently I just use .replace('NUM1', newParam);
but as you can see once 'NUM1'
changes to the new parameter that the varaible newParam
gives it, I can't then change it again as it will no long find 'NUM1'
.
Upvotes: 1
Views: 88
Reputation: 114
If you know all the other aspects of the string, you could do a quick hack and concatenate the string:
str = 'param1=www.DOMAIN.NAME&param2=' + NUM1 + '&param3=true&param4=25'
Not elegant, but it'd work.
Alternatively if that string is variable but the NUM1 is always there, you could split('NUM1'), then join(newParam)
e.g.,
str = 'param1=www.DOMAIN.NAME&param2=NUM1&param3=true&param4=25'
str.split('NUM1').join(newParam);
Upvotes: 2
Reputation: 14736
If I were you I would look into a jQuery plugin for handling query strings. Surely this will always be a querystring-like string. A little searching turned up this, but these kinds of plugins have been around a long time and you're likely to find one that precisely suits your needs.
Upvotes: 0