Suppose I have the following HTML structure:
<test>
<div>
This is a test
</div>
<div>
This is another test
<button>
Button test
</button>
</div>
</test>
Now I use the following jQuery code to replace, e.g., 'T':
$("test *").each(function(index, value) {
$(this).html($(this).html().replace(new RegExp('t', "ig"), "<b>t</b>"));
});
However, this results in the following HTML structure (which is unexpected, see the <button>
tag, which breaks my HTML):
<test>
<div>
<b>T</b>his is a <b>t</b>es<b>t</b>
</div>
<div>
<b>T</b>his is ano<b>t</b>her <b>t</b>es<b>t</b>
<bu<b>t</b><b>t</b>on>
Bu<b>t</b><b>t</b>on <b>t</b>es<b>t</b>
</bu<b>t</b><b>t</b>on>
</div>
</test>
What I want to achieve is:
<test>
<div>
<b>T</b>his is a <b>t</b>es<b>t</b>
</div>
<div>
<b>T</b>his is ano<b>t</b>her <b>t</b>es<b>t</b>
<button>
Bu<b>t</b><b>t</b>on <b>t</b>es<b>t</b>
</button>
</div>
</test>
Basically, I want to replace within the entire element but preserve the HTML tags and all the HTML attributes.