I\'m using a jquery function I found to find words in a div and highlight them. I\'m using this along with a search tool so the case is not always going to match the words
Just add the 'i' flag.
pattern = new RegExp('(>[^<.]*)(' + what + ')([^<.]*)','gi')
pattern = new RegExp('(>[^<.]*)(' + what + ')([^<.]*)','gi')
add the 'i' flag to make it case insensitive
$.fn.highlight = function(what,spanClass) {
return this.each(function(){
var container = this,
content = container.innerHTML,
pattern = new RegExp('(>[^<.]*)(' + what + ')([^<.]*)','gi'),
replaceWith = '$1<span ' + ( spanClass ? 'class="' + spanClass + '"' : '' ) + '">$2</span>$3',
highlighted = content.replace(pattern,replaceWith);
container.innerHTML = highlighted;
});
}
Just add "i":
pattern = new RegExp('(>[^<.]*)(' + what + ')([^<.]*)','gi'),
From MDN:
Regular expressions have four optional flags that allow for global and case insensitive searching. To indicate a global search, use the g flag. To indicate a case-insensitive search, use the i flag. To indicate a multi-line search, use the m flag. To perform a "sticky" search, that matches starting at the current position in the target string, use the y flag. These flags can be used separately or together in any order, and are included as part of the regular expression.