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 exactly. How can I convert this to make it case insensitive?
$.fn.highlight = function(what,spanClass) {
return this.each(function(){
var container = this,
content = container.innerHTML,
pattern = new RegExp('(>[^<.]*)(' + what + ')([^<.]*)','g'),
replaceWith = '$1<span ' + ( spanClass ? 'class="' + spanClass + '"' : '' ) + '">$2</span>$3',
highlighted = content.replace(pattern,replaceWith);
container.innerHTML = highlighted;
});
}
pattern = new RegExp('(>[^<.]*)(' + what + ')([^<.]*)','gi')
add the 'i' flag to make it case insensitive
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With