Skip to content

Instantly share code, notes, and snippets.

@eserge
Created February 3, 2012 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eserge/1729974 to your computer and use it in GitHub Desktop.
Save eserge/1729974 to your computer and use it in GitHub Desktop.
highlight a 'part' part of a given string ('highlight_string')
function hightlight(highlight_string, part){
workstring = highlight_string;
var indices = [];
var cut = all_cut = 0;
while (workstring.indexOf(part) != -1) {
index = workstring.indexOf(part);
indices.push(index+all_cut);
cut = index + part.length;
workstring = workstring.substr(cut);
all_cut = all_cut + cut;
}
var opentag = "<ins>";
var closetag = "</ins>";
for (var i in indices) {
var offset = (opentag+closetag).length*i;
highlight_string = highlight_string.substring(0, indices[i]+offset) +
opentag +
highlight_string.substring(indices[i]+offset, indices[i]+part.length+offset) +
closetag +
highlight_string.substring(indices[i]+part.length+offset);
}
return highlight_string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment