This code can be used as-is, just copy and paste. The following is a list of what the changes to the original code will do:
It will highlight only whole words
Changes to function highlightNodes(htmlBlock, searchTerms) allow only the highlighted term and it's plural versions to be highlighted, so if your search criteria is ice, the ice in rice will not be highlighted but ices will be highlighted.
function isAlphaNumeric(str) was added to test for alphanumeric characters surrounding the search criteria. Revised on Nov 11, 2008 because Firefox will not work correctly work with the regular expression unless is is specifically declared as a RegExp object. Revised July 16, 2009 because if a word to be highlighted was the first word following <span> in record display, it was not being highlighted because isAlphaNumeric(str) was returning true for empty strings.
Prevent highlighting of certain words
Changes to function highlightResultSet(searchTerms) and highlightRecordDisplay(searchTerms) make it so certain search terms will not be highlighted. The words that will not be highlighted are defined in an array called stop_words.
To add your own stop words, look for the line var stop_words = new Array('is','to','the','for','in'); and add your own words in the same format as the existing words. (don't forget your single quotes and commas!)
function array_contains() was added to compare the search terms to the words in the array.
Highlighting quoted phrases as quoted phrases only, not as individual words.
Changes to function highlightResultSet(searchTerms) and highlightRecordDisplay(searchTerms) make it so quoted phrases, like "foreign policy", will be highlighted as phrases. In the "foreign policy" example, foreign will not be highlighted unless it is followed by policy, and policy will only be highlighted if the preceding word is foreign.
Copy and paste the code below or download the highlight script.