javascript - underscore.js removing text from string based on array of words -
having little issue logic , underscore.js.
i have next array of mutual words:
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"];
and have next string beingness passed in:
this printer has words on
now should happen mutual words should stripped out leaving words "printer" , "words".
i have tried using underscore.js below not working expected , returning matches common_words array.
$('.tagtrigger').blur(function () { var subjectval = $('#txtsubject').val().split(/ +/); $.each(subjectval, function (index, itm) { console.log(itm); console.log(_.reject(common_words, function (item) { homecoming item != itm })); }); });
any help appreciated, need pair of eyes guess :-)
now should happen mutual words should stripped out leaving words "printer" , "words".
i don't think need underscorejs here. next suffice.
var subjectval = "this printer has words on it"; subjectval = subjectval.replace(new regexp("\\b(" + common_words.join("|") + ")\\b", "g"), "");
the above leave words spaces, can removed
var result = subjectval.replace(/\s+/g," ").trim();
javascript arrays underscore.js
No comments:
Post a Comment