Thursday, 13 December 2018

javascript - JS highlight matched content x times

Answer


How can you highlight x occurrences



How can you highlight, in pure JS only, a limited subset of matches from a set of text so that only x number of highlights occur per match.




var matches = new Array('fox', 'dog');
var MaxHighlights = 2;


Original content



The quick brown fox jumps over the lazy dog but the lazy dog is quick of the mark to catch the brown fox. In general the fox versus the dog is not a good match.



Highlighted content




The quick brown fox jumps over the lazy dog but the lazy dog is quick of the mark to catch the brown fox. In general the fox versus the dog is not a good match.






For extra points I'd preferably only highlight one match per sentence.



Preferred Highlighted content



The quick brown fox jumps over the lazy dog but the lazy dog is quick of the mark to catch the brown fox. In general the fox versus the dog is not a good match.




I was using this is a base for my highlighting attempts
http://www.the-art-of-web.com/javascript/search-highlight

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...