Created
February 3, 2012 12:37
-
-
Save eserge/1729974 to your computer and use it in GitHub Desktop.
highlight a 'part' part of a given string ('highlight_string')
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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