JSFiddle - React, Tailwind, and code Playground
JavaScript
h= '<span myAtrib="something"><a href="old(this SHOULD NOT be changed)">old(this SHOULD be changed)</a></span>';
function replace($element, from, to) {
$element.contents().each(function() {
if (this.nodeType === 3) { // text node
this.nodeValue = this.nodeValue.replace(from, to);
}
else if (this.nodeType === 1) { // element node
replace($(this), from, to);
}
});
}
var $ele = $("<div>"+ h +"</div>").find('span[myAtrib="something"]')
replace($ele, /old/g, "new");
var html = $ele.end().html();
alert(html);