jQuery.highlightRegex
jQuery.highlightRegex copied to clipboard
Added palette feature
Added palette feature so that each match group can be styled differently.
This seems like fairly application-specific functionality unsuited for a library. Why is this preferable to the following, which requires no modification to the library:
var cyclingClasses = ['a', 'b', 'c', 'd'];
$('#content')
.highlightRegex(/some regex/ig)
.find('.highlight')
.each(function(i) {
$(this).addClass(cyclingClasses[i % cyclingClasses.length])
})
You're right, it is too specific, but I couldn't think of a better way to get my results.
Your solution is nice and neat, but it just cycles the colours for each highlight. My solution colours each match group with the same colour.
I require all instances of a specific keyword have the same colour, as I am highlighting multiple keywords – actually groups of keywords – in the same document with one complex regular expression. I was doing it with multiple calls to highlightRegex, but that was too slow on long pages. So I arrived at my solution in order to do it in one call, and get match group specific colours.
For example:
/(Linux|Ubuntu)|(Windows|PC)|(Mac|Apple|iPad)/gi
...would colour each group of keywords in their own colour.
See this topic for background on the basis of my patch: http://stackoverflow.com/questions/14912419/get-the-index-of-the-group-that-matched-in-a-regexp/14912534#14912534