<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf⑻" />
<title>无标题文档</title>
</head>
<script type="text/javascript">
var countWorld=function(){
var text = document.getElementById("worldText").value;
//把非字母替换成@
text = text.replace(/[^a-zA-Z]+/g,'@');
console.log(text.split('@'));
//将替换后句子转为数组
var arr = text.split('@');
var count=0;
var result='';
for(var i=0;i<arr.length;i++){
var a = arr[i];
for(var j=0;j<arr.length;j++){
var b = arr[j];
if(a==b){
count++;
}
}
//如果结果不包括,即没有统计过
if(result.indexOf(a)==⑴){
result+=a+' ';
result+=count+',';
}
//重置为0
count =0;
}
console.log(result);
}
</script>
<body>
请输入句子:<input type="text" id="worldText" />
<input type="button" onclick="countWorld()" value="统计单词"/>
</body>
</html>