JavaScript关键字检查器

JavaScript关键字检查器,javascript,jquery,keyword,Javascript,Jquery,Keyword,我有一个脚本,它根据关键字数组检查大量文本,然后返回计数。我现在需要它能够做的是过滤掉可能已经包含另一个关键字的关键字。例如: 苹果很好吃,尤其是红苹果 当前计数如下所示: apples - 2 (counts "apples" twice) red apples - 1 apples - 1 red apples - 1 我想让关键字独立,如下所示: apples - 2 (counts "apples" twice) red apples - 1 apples - 1 red appl

我有一个脚本,它根据关键字数组检查大量文本,然后返回计数。我现在需要它能够做的是过滤掉可能已经包含另一个关键字的关键字。例如:

苹果很好吃,尤其是红苹果

当前计数如下所示:

apples - 2 (counts "apples" twice)
red apples - 1
apples - 1
red apples - 1
我想让关键字独立,如下所示:

apples - 2 (counts "apples" twice)
red apples - 1
apples - 1
red apples - 1
检查关键字的基本脚本:

content = ed.getContent().toLowerCase();
var words = ["apples", "red apples"];       
var count = [];                         

for (var i = 0, len = words.length; i < len; i++) {
    if (text.indexOf(words[i].toLowerCase()) > -1){
        var regex = new RegExp(words[i], "g");
        count[i] = (content.match(regex) || []).length;
        console.log(words[i] + " " + count[i]);
    }
}
content=ed.getContent().toLowerCase();
var words=[“苹果”、“红苹果”];
var计数=[];
for(var i=0,len=words.length;i-1){
var regex=新的RegExp(字[i],“g”);
计数[i]=(content.match(regex)| |[])长度;
console.log(字[i]+“”+count[i]);
}
}

我卡住了!任何朝着正确方向的帮助或推动都是非常感激的

有很多方法可以做到这一点。我认为最简单的方法是对单词进行排序并删减内容


有很多方法可以做到这一点。我认为最简单的方法是对单词进行排序并删减内容


这真的很有帮助,谢谢。我很难理解这个想法,但是看看你是如何按照字符串长度顺序排序的,这是非常有意义的。非常感谢。这真的很有帮助,谢谢。我很难理解这个想法,但是看看你是如何按照字符串长度顺序排序的,这是非常有意义的。非常感谢。