I want to replace a word that is in html content (not attributes) and I’ve googled and found variant regular expressions, but none of them could not cover all cases.
for example replace “width” to “[width]”
<div style="width: 200px">
width is 300.
<div>
<div style="width: 100px">
width is 200.
</div>
</div>
</div>
to
<div style="width: 200px">
[width] is 300.
<div>
<div style="width: 100px">
[width] is 200.
</div>
</div>
</div>
Thanks in advance
Example Live example
As I already said in my comment, iterate over all text nodes and replace their content.
Assuming
elementis a reference to the root:DEMO
If you haven’t parsed the HTML yet, you can create a temporary element:
Something similar can be done with jQuery using
.contents()[docs] to get all child nodes, including text nodes.