<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
body,html{
font-family: "微软雅黑"
}
li{
margin:0
background: #aaa
font-size: 15px
line-height: 2
font-weight: bold
font-variant:small-caps
list-style:none
display: inline-block
margin-right: 15px
margin-top:15px
padding:0 15px
color: #FFF
box-shadow: 0px 0px 10px 0px #000
text-transform:capitalize
}
h6{
font-size: 16px
color: red
box-shadow: 0px 0px 10px 0px #000
text-transform:capitalize
font-variant:small-caps
padding: 10px 0
text-indent: 15px
}
</style>
<body>
<h2>
javascript一睁亏歼段英文文章统计文章中每个单词悉冲出现的次数,
找出文章中最长的单词并统计其出现次数最多的单词
</h2>
<p>
《You Have Only One Life》
There are moments in life when you miss someonessosomeonesso much that you just want to pick them from your dreams and hug them for real! Dream what you want to dreamgo where you want to gobe what you want to be,because you have only one life and one chance to do all the things you want to do.
May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you 空告feel that it hurts you,it probably hurts the other person, too.
The happiest of people don’t necessarily have the best of everythingthey just make the most of everything that comes along their way.Happiness lies for those who cry,those who hurt, those who have searched,and those who have tried,for only they can appreciate the importance of people
who have touched their lives.Love begins with a smile,grows with a kiss and ends with a tear.The brightest future will always be based on a forgotten past, you can’t go on well in lifeuntil you let go of your past failures and heartaches.
When you were born,you were crying and everyone around you was smiling.Live your life so that when you die,you're the one who is smiling and everyone around you is crying.
Please send this message to those people who mean something to you,to those who have touched your life in one way or another,to those who make you smile when you really need it,to those that make you see the brighter side of things when you are really down,to those who you want to let them know that you appreciate their friendship.And if you don’t, don’t worry,nothing bad will happen to you,you will just miss out on the opportunity to brighten someone’s day with this message.
</p>
<script>
(function() {
//var r_word=/(\s+|[\.,!《》])([a-zA-Z]*)/g
var r_word = /\b\w+’*\w*\b/g
var s_word = document.querySelector("p").innerHTML
var a_everyword = s_word.match(r_word)
var a_length_of_everyword = []
var j_length_of_everyword = {}
a_everyword.forEach(function(o, i) {
a_length_of_everyword.push(o.length + "_" + i)
})
a_length_of_everyword.sort(function(a, b) {
return parseInt(b) - parseInt(a)
})
var max_length = a_length_of_everyword[0].split("_")[0]
var length = max_length
var index = 0
var most_same_word
var most_same_word_count=0
function json_length_of_everyword() {
while (a_length_of_everyword[index] && a_length_of_everyword[index].split("_")[0] == length) {
!j_length_of_everyword[length] && (j_length_of_everyword[length] = [])
j_length_of_everyword[length].push(a_everyword[a_length_of_everyword[index].split("_")[1]])
index++
}
j_length_of_everyword[length]&&j_length_of_everyword[length].sort()
length--
if (length == 0) {
count()
return
} else {
json_length_of_everyword()
}
}
json_length_of_everyword()
function count() {
var cur_word, cur_word_first_index, j = 1,same_count
for ( j < Number(max_length) + 1 j++) {
j_length_of_everyword[j]&&j_length_of_everyword[j].forEach(function(o, i) {
i == 0 && (cur_word_first_index = i, cur_word = o)&& j == max_length && f_max_length_word_info("本文最长单词" + cur_word + "长度" + j)
o != cur_word && (same_count=i - cur_word_first_index)&&f_everyword_info("单词 '"+cur_word + "' 出现了" + same_count + "次") && (cur_word = o, cur_word_first_index = i) && j == max_length && f_max_length_word_info("本文最长单词" + cur_word + "长度" + j)
!j_length_of_everyword[j][i + 1] && (same_count=i - cur_word_first_index+1) && f_everyword_info("单词'"+cur_word + "'出现了" + same_count + "次")
if(same_count>most_same_word_count){
most_same_word_count=same_count
most_same_word=cur_word
}
})
}
f_most_same_word(most_same_word_count,most_same_word)
}
function f_everyword_info(str) {
document.write('<li>'+str+'</li>')
return true
}
function f_max_length_word_info(str) {
document.write('<h6>'+str+'</h6>')
return true
}
function f_most_same_word(n,s){
document.write('<h6>最多重复单词'+s+'出现次数'+n+'次</h6>')
}
})()
</script>
</body>
</html>
<script>var str = "this is 启纤my book."
alert(str.match(/\bis\b/gi)) //结果:is, this中的is并者禅不会首旁尘被查到
</script>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)