Hola @
NSD el tema es un poco complicado, pero a partir de
esta idea sale el siguiente código:
Código Javascript
:
Ver originalvar matchText = function(node, regex, callback) {
var child = node.firstChild;
do {
if (child.nodeType === 3) {
child.data.replace(regex, function(all) {
var args = [].slice.call(arguments),
offset = args[args.length - 2];
if(offset > child.data.length){
offset = child.data.indexOf(all);
}
var newTextNode = child.splitText(offset);
newTextNode.data = newTextNode.data.substr(all.length);
callback.apply(window, [child].concat(args));
child = newTextNode;
});
}
} while (child = child.nextSibling);
return node;
}
var searchTerm = 'texto';
matchText(document.getElementById("textoid"), new RegExp("\\b" + searchTerm + "\\b", "g"), function(node, match, offset) {
var span = document.createElement("span");
span.textContent = match;
node.parentNode.insertBefore(span, node.nextSibling);
});
DEMO
Saludos