JS Find the most frequently occurring words in English articles

  • 2021-08-05 08:11:47
  • OfStack

The following function is js to find the word with the highest frequency in an English article (composed of 26 English letters in upper and lower case), and output the word and the number of occurrences, which is insensitive to case and mainly uses regularity:

function counts(article){
 article = article.trim().toUpperCase();
 var array = article.match(/[A-z]+/g);
 article = " "+array.join(" ")+" ";
 var max = 0,word,num = 0,maxword="";
 for(var i = 0; i < array.length; i++) {  
  word = new RegExp(" "+array[i]+" ",'g');
 num = article.match(word).length;
  maxword = array[i];
 console.log(maxword+" "+max);
counts("Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the same day;");

Related articles: