使Javascript正则表达式不区分大小写 [英] Make Javascript regular expression case insensitive
本文介绍了使Javascript正则表达式不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用一个jquery函数,我发现在div中找到单词并突出显示它们。我正在使用它和搜索工具,因此案例并不总是与单词完全匹配。如何将其转换为不区分大小写?
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')
添加'i'标志使其不区分大小写
这篇关于使Javascript正则表达式不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文