Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Javascript regular expression case insensitive

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;
    });
}
like image 543
Adam Avatar asked Nov 30 '22 03:11

Adam


1 Answers

pattern = new RegExp('(>[^<.]*)(' + what + ')([^<.]*)','gi')

add the 'i' flag to make it case insensitive

like image 50
Joseph Avatar answered Dec 06 '22 08:12

Joseph