Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
78
rated 0 times [  82] [ 4]  / answers: 1 / hits: 18182  / 12 Years ago, fri, march 23, 2012, 12:00:00

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;
});
}

More From » jquery

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


add the 'i' flag to make it case insensitive


[#86655] Thursday, March 22, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mariamiyanab

Total Points: 75
Total Questions: 102
Total Answers: 92

Location: British Indian Ocean Territory
Member since Tue, Feb 22, 2022
2 Years ago
;