JSFiddle - React, Tailwind, and code Playground
HTML
<a href="/shop/product?ie=UTF8&asin=Z00FDLN878&tab=UK_Default"
target="_blank">
<img width="125" height="125" border="0" src="http://ecx.images-amazon.com/images/I/01W9a7gwosL.jpg" alt="43453">
</a>
<hr/>
<a href="#asin=sdfwe">
<span>something that isn't an image</span>
</a>
<br/>
<hr/>
<a href="#asin=sdsdsfwfe">
text
</a>
JavaScript
// the new base url
var base = ' https://www.example.co.uk/gp/wine/order?ie=UTF8&asin=';
var links = document.getElementsByTagName('a');
for(var i = 0;i < links.length;i++){
var linkElement = links[i];
var firstChild = linkElement.children[0];
if (typeof(firstChild) !== "undefined" && firstChild.tagName=="IMG") {
// check each link for the 'asin' value
var result = /asin=([\d\w]+)/.exec(links[i].getAttribute('href'));
if(result){
// make a new url using the 'base' and the 'asin' value
links[i].setAttribute('href', base+result[1]);
}}
}