Ask HN: Efficient HTML Text Replace, RegEx? jQuery?
So, the other day, i wrote a line of jQuery like so to find all elements that contained the word 'apple'. the idea was that i could do the find/replace faster than doing it via a regex over the whole doc, which requires a redraw.
$("*:contains['apple']").each( function(){ //do something });
the result was that i was processing iteratively on every item in the DOM that had a descendant which contained the word 'apple', rather than just the child objects.
I ended up using some code from Johann Burkard http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html
Johann's approach iterates through the text using a string.indexOf(pat) pattern. i was wondering if any of you had any better ideas?
In the ideal situation, i would use a jQuery selector to select only child nodes that contained 'apple', but there is no such selector 'isChild' that can be paired with :contains as far as i know.
I would like to replace all instances of 'apple' with '<span class="highlight">apple</span>" with the lowest impact to the dom possible.