Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery过滤器和高亮显示表_Javascript_Jquery_Search_Filter_Highlight - Fatal编程技术网

Javascript jQuery过滤器和高亮显示表

Javascript jQuery过滤器和高亮显示表,javascript,jquery,search,filter,highlight,Javascript,Jquery,Search,Filter,Highlight,我的代码有一些问题。在搜索时,我试图查找字母或单词并突出显示它,但有一些问题,例如,当我搜索单词“Aram”时,它返回我“Aram”。当在一个词中,我有更多相同的字母,第一个是大写字母,所有其他字母替换为大写字母。你能检查一下我的密码并说出我做错了什么吗 example 'Aram' -> 'ArAm(<mark>A</mark>r<mark>A</mark>m)' but shuld be 'Aram(<mark>A</m

我的代码有一些问题。在搜索时,我试图查找字母或单词并突出显示它,但有一些问题,例如,当我搜索单词“Aram”时,它返回我“Aram”。当在一个词中,我有更多相同的字母,第一个是大写字母,所有其他字母替换为大写字母。你能检查一下我的密码并说出我做错了什么吗

example 'Aram' -> 'ArAm(<mark>A</mark>r<mark>A</mark>m)' but shuld be 'Aram(<mark>A</mark>r<mark>a</mark>m)'
示例'Aram'->'Aram(Aram)'但应为'Aram(Aram)'
JavaScript:

$("input").on("keyup", function () {
    var valThis = this.value;


    $('table').find('tr td').each(function () {
        if($(this).attr('data-search') !== 'false') {
            console.log('');


            var text = $(this).text();
            var textL = text.toLowerCase();
            var position = textL.indexOf(valThis.toLowerCase());

            if (position !== -1) {
                var matches = text.substring(position, ( valThis.length + position ));

                var regex = new RegExp(matches, 'ig');

                var highlighted = text.replace(regex, `<mark>${matches}</mark>`);

                console.log(highlighted);

                $(this).html(highlighted);

                setTimeout(function () {
                    if($(this).parent().find('mark').is(':empty')) {
                        $('mark').remove();
                    }
                }.bind(this),0);
            } else {
                console.log('sadasdasd');
                $(this).text(text);
            }
        }

        if($(this).parent().find('mark').length > 0) {
            $(this).parent().show();
        }else {
            $(this).parent().hide();
        }
    });
});
$(“输入”)。在(“键控”上,函数(){
var valThis=此值;
$('table')。查找('tr td')。每个(函数(){
if($(this.attr('data-search')!='false'){
控制台日志(“”);
var text=$(this.text();
var textL=text.toLowerCase();
var position=textL.indexOf(valThis.toLowerCase());
如果(位置!=-1){
var matches=text.substring(位置,(valThis.length+position));
var regex=新的RegExp(匹配“ig”);
var highlighted=text.replace(regex,`${matches}`);
console.log(突出显示);
$(this.html)(突出显示);
setTimeout(函数(){
if($(this).parent().find('mark')。为(':empty')){
$('mark')。删除();
}
}.bind(this),0);
}否则{
console.log('sadasdasd');
$(此).text(文本);
}
}
if($(this).parent().find('mark')。长度>0){
$(this.parent().show();
}否则{
$(this.parent().hide();
}
});
});
这是我的

感谢您的帮助

试试这个:

var regex = new RegExp(valThis, 'ig');
text = text.replace(regex, (match, $1) => {
  // Return the replacement
  return '<mark>' + match + '</mark>';
});

$(this).html(text);
var regex=new RegExp(valThis,'ig');
text=text.replace(正则表达式,(匹配,$1)=>{
//退回替代品
返回“”+匹配+“”;
});
$(this).html(文本);