I've been looking for an efficient way to do this but haven't been able to find it, basically what I need is that given this url for example:
http://localhost/mysite/includes/phpThumb.php?src=http://media2.jupix.co.uk/v3/clients/4/properties/795/IMG_795_1_large.jpg&w=592&aoe=1&q=100
I'd like to be able to change the URL in the src
parameter with another value using javascript or jquery, is this possible?
Thanks in advance.
In addition to @stenix, this worked perfectly to me
Here no need to save the "url" variable, just replace in current url
The following solution combines other answers and handles some special cases:
?
character\b
ensures another parameter ending with paramName won't be matchedSolution:
Known limitations:
Wouldn't this be a better solution?
EDIT:
added some clarity in code and kept 'src' in the resulting link
$1
represents first part within the()
(i.e)src=
and$2
represents the second part within the()
(i.e)&
, so this indicates you are going to change the value betweensrc
and&
. More clear, it should be like this:ADD-ON for replacing all the ocurrences:
If you have several parameters with the same name, you can append to the regex global flag, like this
text.replace(/(src=).*?(&)/g,'$1' + newSrc + '$2');
and that will replaces all the values for those params that shares the same name.Here is modified stenix's code, it's not perfect but it handles cases where there is a param in url that contains provided parameter, like:
In this case searchquery param value is changed.
Code:
How about something like this: