underscore.js根据单词数组从字符串中删除文本

时间:2014-11-01 13:13:59

标签: 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", "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

现在应该发生的是,所有常见的单词都应该被删除,只留下“打印机”和“单词”这两个词。

我尝试使用下面的underscore.js,但它没有按预期工作,只是返回与common_words数组匹配的所有内容。

$('.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 }));
        });
    });

任何对此的帮助都会非常感激,只需要另外一双眼睛我想: - )

3 个答案:

答案 0 :(得分:3)

  

现在应该发生的是所有常用词应该得到   剥离出简单的单词" printer"和"单词"。

我认为你不需要这里的下划线。以下就足够了。

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();

答案 1 :(得分:1)

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

过滤它来使用纯Javascript
"this is the printer that has the words on it".split(" ").filter(function(word){
    return common_words.indexOf(word) === -1;
}).join(' ');

结果

"printer words"

答案 2 :(得分:1)

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

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", "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"];

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);

document.getElementById("uncommonText").innerHTML = uncommonWords.join(' ')

<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>

<p id='uncommonText'></p>
&#13;
&#13;
&#13;