I'm trying to build a small Javascript application which loads a third party site, finds a given word and highlights the closest context of the document using the Jquery extension Highlight (with a small customization for allowing regular expressions).
First, I'm trying to get the application to highlight surrounding by setting the context to 500 characters, but for some reason it cuts off in weird places. For this article, I'm trying to match the term Obama, and as you can see from my screenshot, it cuts off in places where it shouldn't be.
Does anyone have any clue of what's going on?
$(document).ready(function() {
$.get(getUrlVars()["url"],
function(data) {
var fdata = $(data);
var associationScope= 500;
$.each(getUrlVars()["topics"].split(","), function(index, value) {
if (getUrlVars()["associationScope"] == "context") {
var associationScopeRegex = "((?!</span>)[\\s\\S]{0," + associationScope + "})"
+ value + "((?!<span class=\"associationScope\">)[\\s\\S]{0," + associationScope + "})";
fdata.highlight(associationScopeRegex, {className: "associationScope"});
}
fdata.highlight(value, {className: "topicHighlight"});
});
$("#externalPage").html(fdata);
});
});