I am trying to replace all text between tags, and I want to find out the fastest way to do this.
An example would be an attempt to replace all text with an arbitrary string helloWorld, so that this:
<div>
<div>
RandomText1
<div>
RandomText2
</div>
</div>
</div>
Becomes as follows:
<div>
<div>
helloWorld
<div>
helloWorld
</div>
</div>
</div>
My current approach:
- Do a depth search (DFS) in the DOM
- For each element, analyze and determine which part is the text and which part is the element.
- For the part that replaces the text.
This would be very slow for me, especially trying to do this for a large document and repeat this process many times. Is there a faster way?
source
share