Here's a completely clean JavaScript solution to complement Patrick's answer.
//specify a start point recurseDOM(document.body); function recurseDOM(scope) { var i = 0, nodes, node; if(scope.childNodes) { nodes = scope.childNodes; for(i;i<nodes.length;i++) { node = nodes[i]; if(node.nodeType === 3) { //is a text node checkTextNode(node); } if(node.childNodes) { //loop through child nodes if child nodes are found recurseDOM(node); } node = null; } nodes = null; } } function checkTextNode(node) { var newText = "is", patt = /is/g, text = node.data, test = patt.test(text); if(test) { //match found, replace node textual data with specified string node.data = text.replace(patt, "__" + newText + "__"); newText = null; text = null; } test = null; }
For fun, here is the code in the sandbox: http://jsfiddle.net/mkmcdonald/th6bh/1/
user1385191
source share