admin管理员组文章数量:1404579
In my Node.js app, I have a list of words in an array I'd like to see if are in any random string (actually, a tweet.) I don't care necessarily which words appear and how often they appear, I just want a number of times ANY of the words show up in the string.
So, for instance if I have an array:
search = ['foo', 'bar', 'roger'];
and I have a string "foo bar"
, I'd like a response of 2
. If I have a string "hello world, roger"
,
I'd like a response of 1
. Etcetera.
What's the best way to do this? Regular expressions? Some other black magic?
It seems like it should be easy, but I haven't the faintest idea about how to phrase it in a way that makes Google understand it. ;)
In my Node.js app, I have a list of words in an array I'd like to see if are in any random string (actually, a tweet.) I don't care necessarily which words appear and how often they appear, I just want a number of times ANY of the words show up in the string.
So, for instance if I have an array:
search = ['foo', 'bar', 'roger'];
and I have a string "foo bar"
, I'd like a response of 2
. If I have a string "hello world, roger"
,
I'd like a response of 1
. Etcetera.
What's the best way to do this? Regular expressions? Some other black magic?
It seems like it should be easy, but I haven't the faintest idea about how to phrase it in a way that makes Google understand it. ;)
Share Improve this question asked Feb 23, 2014 at 15:25 Brett NeeseBrett Neese 472 silver badges8 bronze badges 1- I think you need to define the problem better. How many words are you interested in? Are you interested in whole words, related words (say - work, working, worker), any substring? We know that you're interested in tweets, but how many tweets are you going to examine? How many words will you be looking for? The answers to these, and probably more, questions will help define how you'd go about doing the search. – tvanfosson Commented Feb 23, 2014 at 15:39
4 Answers
Reset to default 4What's the best way to do this? Regular expressions? Some other black magic?
No black magic here, just regular expressions.
"hello world, roger".match(/foo|bar|roger/g).length
should do it.
One note: Regular expressions are really fast at doing plex things, but are really slow when doing lots of simple things. In this case if the searchString is large, I would try to avoid created a plex regular expression.
First, convert the search into a hash:
var search = ['foo', 'bar', 'roger'];
var searchHash = {}, i=0;
for (i=0;i<search.length;i++){
searchHash[search[i]]=true;
}
Then, split the input string into a series of words:
var inputWords = "foo bar".split(" ");
Now, search for the frequency of each word:
var wordFreq = {}, var total=0;
for (i=0;i<inputWords.length;i++){
//Check to see if this word is in our hash
if (searchHash[inputWords[i]]) {
//If it is add one to its count
wordFreq[inputWords[i]] = wordFreq[inputWords[i]] || 0;
wordFreq[inputWords[i]]++;
total++;
}
}
Last, output all the words with their frequency:
Object.keys(wordFreq).forEach(function(key, index){
console.log(key + ": "+ wordFreq[key]);
});
console.log("Total matches: "+total);
A functional way, with Array.prototype.reduce
var search = ['bar', 'foo', 'roger'];
var my_string = "foo bar";
console.log(search.reduce(function(tillNow, now) {
return tillNow + (my_string.indexOf(now) !== -1);
}, 0));
A more reusable answer based alex's answer would be :
searchString = 'hello world, roger';
searchTokens = ['foo', 'bar', 'roger'];
searchRegex = new RegExp(searchTokens.join('|'), 'g');
numOfMatches = searchString.match(searchRegex).length;
本文标签: javascriptSearching for list of words occurrences in a stringStack Overflow
版权声明:本文标题:javascript - Searching for list of words occurrences in a string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744840311a2627884.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论