I'm trying to parse a text as html, making some changes, and then again turning in back to plain text.
The changes that I'm going to make is bassically replacing some term with another (e.g. 'old'
with 'new'
).
I only want to change the text types, not the attributes and properties of html elements (e.g. the href
values should not be changed.).
Please see the code:
h= '<span myAtrib="something"><a href="old(this SHOULD NOT be changed)">old(this SHOULD be changed)</a></span>';
h=$("<div>"+ h +"</div>").find('span[myAtrib="something"]').html(function (i, oldHtml) {
oldHtml = oldHtml.replace(/old/g, 'new');
return oldHtml;
}).end().html();
How should I make the replacement happen only on text nodes, or at least, how should I filter-out the anchor elements?