A small utility to highlight text in a browser
Mark(root[, query, options]);
root: Required. Container element to search/highlight.query: Optional. String to search for. Ifqueryis a non-zero length string the function will mark matching text in therootelement, otherwise it will unmark text.options: Optional. A dictionary object to supply options.
mode: Sets the text matching mode. Default is 0. Themodevalue can be set using the provided constants:Mark.EXCLUSIVE_MATCH = 0: Only matched text will be highlighted.Mark.INCLUSIVE_MATCH = 1: Entire word will be highlighted if it contains a match.Mark.EXACT_MATCH = 2: Entire word will be highlighted if it an exact match.
separate: Boolean value that flags whether to use search each word in the query as a single phrase or individually. Default is false.
If the function is called to mark text it will return an array of mark elements, otherwise the return value is undefined.
// mark all occurrences of 'and'
var marks = Mark(document.body, 'and');
// unmark
Mark(document.body);
// mark all occurrences of words containing 'and'
Mark(document.body, 'and', { mode: Mark.INCLUSIVE_MATCH});
// mark all occurrences of the word 'the'
Mark(document.body, 'the', { mode: Mark.EXACT_MATCH });
// mark all occurrences of multiple words by exact match
Mark(document.body, 'the and yellow', { mode: Mark.EXACT_MATCH, separate: true });
A live demo is available on jsfiddle.