I would have used a regular expression and DOM methods:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="txt/javascript">
<script type="text/javascript">
function replaceText(){
var re=/\*\*+.+\*\*/;
var att=['src','width','height','alt'];
var elems=document.getElementsByTagName('*');
var i=0, e;
while(e=elems[i++]){
var j=0, k, s, p;
while(k=e.childNodes[j++]){
if(k.nodeType==3&&k.nodeValue.match(re)){
if(checkParents(k)){
s=k.nodeValue.split('**')[1].split(' ');
p=document.createElement('img');
setAtt(p,att,s);
k.parentNode.replaceChild(p,k);
}
else{k.data=k.data.replace(re,'');}
}
}
}
}
function checkParents(k){
var p=k.parentNode;
while(p.nodeName!='BODY'){
if(p.nodeName=='H4'){return true}
p=p.parentNode;
}
return false
}
function setAtt(p,att,s){
for(var i=0; i<att.length; i++){
p.setAttribute(att[i],s[i]);
}
}
</script>
</head>
<body>
<button onclick="replaceText();">Replace Text</button>
<br>
<h4><a href="some url">**url/pic.gif 20 25 icon** My link</a></h4>
<br>
<p>**url/pic.gif 20 25 icon** My link</p>
</body>
</html>