I am trying to wrap my head around using regular expressions and replace() with JavaScript, but have not yet been successful
Suppose I have a string containing this:
<img alt="a picture of ..." src="image/1533?foo=1&bar=2$zot=3" width="500" />
If zot=3
I want remove foo (and its value) (or replace foo=x
with an empty string).
The replacement would look like this:
<img alt="a picture of ..." src="picture/1533?bar=2$zot=3" width="500" />
I want it to be as bullet-proof as possible as I can never be sure which order the URL parameters will be given in.
Is this possible using a single regex, or are there better solutions?
I was thinking of using DOM and
- traversing all
img
nodes - get the
src
attribute - do a test if
@src
value haszot=3
- if it has
zot=3
, replacefoo=1
with an empty string
Of course I have to make sure that any ampersand and so on is removed too.
But I hope to resolve it using a regex or two,
Thanks for any and all answers and advice!