Javascript Underline.js基于单词数组从字符串中删除文本

Javascript Underline.js基于单词数组从字符串中删除文本,javascript,arrays,underscore.js,Javascript,Arrays,Underscore.js,对一些逻辑和下划线.js有点小问题 我有以下一系列常用词: var common_words = ["a", "an", "the", "all", "am", "an", "and", "any", "are", "as", "at", "be", "but", "can", "did", "do", "does", "for", "from", "had", "has", "have", "here", "how", "i", "if","in", "is", "it", "no", "no

对一些逻辑和下划线.js有点小问题

我有以下一系列常用词:

var common_words = ["a", "an", "the", "all", "am", "an", "and", "any", "are", "as", "at", "be", "but", "can", "did", "do", "does", "for", "from", "had", "has", "have", "here", "how", "i", "if","in", "is", "it", "no", "not", "of", "on", "or", "so", "that", "the", "then", "there", "this", "to", "too", "up", "use", "what", "when", "where", "who", "why", "you"];
我传入了以下字符串:

this is the printer that has the words on it
现在应该做的是,所有常用词都应该去掉,只留下“打印机”和“单词”

我试过使用下划线.js,如下所示,但它没有按预期工作,只返回与公共_单词数组匹配的所有内容

$('.tagTrigger').blur(function () {
        var subjectVal = $('#txtSubject').val().split(/ +/);

        $.each(subjectVal, function (index, itm) {
            console.log(itm);
            console.log(_.reject(common_words, function (item) { return item != itm }));
        });
    });
如果您能帮上忙,我们将不胜感激,我想您还需要一双眼睛:-)

现在应该发生的是,所有的常用词都应该 只留下“打印机”和“文字”这两个词

我想你不需要在这里加下划线。以下几点就足够了

var subjectVal = "this is the printer that has the words on it";
subjectVal = subjectVal.replace(new RegExp("\\b(" + common_words.join("|") + ")\\b", "g"), "");
上面的内容会留下单词,但带有空格,可以像

var result = subjectVal.replace(/\s+/g," ").trim();
现在应该发生的是,所有的常用词都应该 只留下“打印机”和“文字”这两个词

我想你不需要在这里加下划线。以下几点就足够了

var subjectVal = "this is the printer that has the words on it";
subjectVal = subjectVal.replace(new RegExp("\\b(" + common_words.join("|") + ")\\b", "g"), "");
上面的内容会留下单词,但带有空格,可以像

var result = subjectVal.replace(/\s+/g," ").trim();

你真的不需要下划线。您可以通过拆分句子并使用

结果

"printer words"

你真的不需要下划线。您可以通过拆分句子并使用

结果

"printer words"

派对有点晚了,但这里有一个使用下划线的版本:

var commonWord = function(word){
   return _.contains(common_words, word);
}

var text = "this is the printer that has the words on it";
var words = text.split(' ');

var uncommonWords = _.reject(words, commonWord);
var common_words=[“a”、“an”、“the”、“all”、“am”、“an”、“and”、“any”、“as”、“at”、“be”、“but”、“can”、“did”、“do”、“do”、“for”、“from”、“had”、“has”、“have”、“here”、“how”、“i”、“if”、“in”、“is”、“it”、“no”、“not”、“of”、“on”、“or”、“so”、“that”、“then”、“there”、“this”、“to”、“too”、“up”、“use”、“what”、“where”、“who”、“who”,“为什么”,“你”];
var commonWord=函数(word){
return u.contains(常用词,word);
}
var text=“这是上面有文字的打印机”;
var words=text.split(“”);
var uncommonWords=..reject(words,commonWord);
document.getElementById(“uncommonText”).innerHTML=uncommonWords.join(“”)


派对迟到了一点,但这里有一个使用下划线的版本:

var commonWord = function(word){
   return _.contains(common_words, word);
}

var text = "this is the printer that has the words on it";
var words = text.split(' ');

var uncommonWords = _.reject(words, commonWord);
var common_words=[“a”、“an”、“the”、“all”、“am”、“an”、“and”、“any”、“as”、“at”、“be”、“but”、“can”、“did”、“do”、“do”、“for”、“from”、“had”、“has”、“have”、“here”、“how”、“i”、“if”、“in”、“is”、“it”、“no”、“not”、“of”、“on”、“or”、“so”、“that”、“then”、“there”、“this”、“to”、“too”、“up”、“use”、“what”、“where”、“who”、“who”,“为什么”,“你”];
var commonWord=函数(word){
return u.contains(常用词,word);
}
var text=“这是上面有文字的打印机”;
var words=text.split(“”);
var uncommonWords=..reject(words,commonWord);
document.getElementById(“uncommonText”).innerHTML=uncommonWords.join(“”)


是的,我认为更改是必要的,但我认为您需要在单词-“\\b(“+…+”)\\b”周围添加括号,对吗?我没有想过尝试此方法,而且确实有效,但是。。。。这就是返回的结果:“s prnter wds t”我猜这里发生的事情是,像“I”和“a”这样的单个字母被从非常用词中剥离出来。这是下划线.js的问题,而不是angularjs@OPSoftware查看我当前的代码。。这不应该发生是的,我认为改变是必要的,但我认为你需要在单词-“\\b”(“+…+”)\\b”周围加上括号,对吗?我没有想过尝试这个,它确实有效,但是。。。。这就是返回的结果:“s prnter wds t”我猜这里发生的事情是,像“I”和“a”这样的单个字母被从非常用词中剥离出来。这是下划线.js的问题,而不是angularjs@OPSoftware查看我当前的代码。。这不应该发生,我最终使用了这个,因为我们在项目中已经有了underline.js,这似乎是一个更干净、更快的解决方案。我已经实现了@AmitJoki-answer,效果很好,但由于他是第一个提出解决方案的人,所以我把公认的答案留给了他。实际上,我最终使用了这个答案,因为我们在项目中已经有了下划线.js,这似乎是一个更干净、更快的解决方案。我已经实现了@AmitJoki-answer,效果很好,但由于他是第一个提出解决方案的人,所以我把公认的答案留给了他。