下面這個函數(shù)是js查找一篇英文文章中出現(xiàn)頻率最高的單詞(由26個英文字母大小寫構成),輸出該單詞及出現(xiàn)次數(shù),不區(qū)分大小寫,主要是正則的運用:
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; if(num>max){ max=num; 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;");以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。
新聞熱點
疑難解答
圖片精選