Jquery匹配文本字符串但如果短语的一部分则排除一些

时间:2015-11-09 00:07:29

标签: jquery regex

我有一个项目,我需要识别性别特定的术语,但如果它包含在短语列表中,则忽略匹配。我可以得到以下工作,我会假设效率非常低,但如果我尝试在列表中添加更多的术语,我认为我超过了该行的字符长度。我怎么能更好地编码,是否可以有两个独立的数组 - 一个用于搜索的单词列表(包括单词边界),另一个用于排除短语列表?确定匹配后,它会以红色框突出显示。感谢。

$( '#body' ).filter(function(index,obj) {
            if(/\b(boy)\b|\b(girl)\b|\b(son)\b|\b(daughter)\b|\b(grandfather)\b|\b(grandmother)\b|\b(father)\b|\b(mother)\b|\b(de facto)\b|\b(de-facto)\b|\b(husband)\b|\b(wife)\b|\b(lady)\b|\b(female)\b|\b(woman)\b|\b(gentleman)\b|\b(male)\b|\b(man)\b|\b(uncle)\b/.test($(obj).clone().removeInnerElements().html().toLowerCase())){

                if(/(Wife Pension|Dad and Partner pay|Isle of Man|What a Man project)/.test($(obj).elements[0].innerHTML)) {
                    return false;
                }
                return true;
            }
            return false;
        },
        $(obj).css("border","solid 3px red");

1 个答案:

答案 0 :(得分:0)

如果有人对此感兴趣,那就是工作解决方案。谢谢Terry回复:

$( '#body' ).filter(function(index,obj){

            var html = $(obj).clone().removeInnerElements().html().toLowerCase();

            var blackList = [
              "boy",  
              "girl",  
              "son",  
              "daughter",  
              "grand\\s?father",  
              "grand\\s?mother",  
              "father",  
              "mother",  
              "de\\s?facto",  
              "husband",  
              "wife",  
              "lady",  
              "female",  
              "woman",  
              "gentleman",  
              "male",  
              "man"  
            ];

            var allowedTerms = [
                "wife\\spension",
                "dad\\sand\\spartner\\spay",
                "isle\\sof\\sman",
                "what\\sa\\sman\\sproject"
            ];

            for (i in allowedTerms) {
                var re = new RegExp("\\b"+allowedTerms[i]+"\\b", "g");
                if (re.test(html)) {
                    html = html.replace(re, "");
                }
            }

            for (i in blackList) {
                var re = new RegExp("\\b"+blackList[i]+"\\b");
                if (re.test(html)) {
                    return true;
                }
            }

            return false;
        },
    $(obj).css("border","solid 3px red");