javascript - Highlighting Exact Text Match -
i have following java userscript:
function highlightword(word) { var xpath = "//text()[contains(., '" + word + "')]"; var texts = document.evaluate(xpath, document.body, null, xpathresult.unordered_node_snapshot_type, null); (n = 0; n < texts.snapshotlength; n++) { var textnode = texts.snapshotitem(n); var p = textnode.parentnode; var = []; var frag = document.createdocumentfragment(); textnode.nodevalue.split(word).foreach(function(text, i) { var node; if (i) { node = document.createelement('span'); node.style.backgroundcolor = 'lightgreen'; node.appendchild(document.createtextnode(word)); frag.appendchild(node); } if (text.length) { frag.appendchild(document.createtextnode(text)); } return a; }); p.replacechild(frag, textnode); } } highlightword('office');
would able me finding solution match exact text of highlightword's add? example, want match "office" , not "officer".
thanks!
this answer
https://stackoverflow.com/a/2994336/2707021
should help. need apply word boundaries pattern match string on line
var xpath = "//text()[contains(., '" + word + "')]";
Comments
Post a Comment