统计文章或句中相同的英文单词数 JS

来源:互联网 发布:泰牛程序员 破解 编辑:程序博客网 时间:2024/04/29 17:37
<!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-8" /><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)==-1){result+=a+' ';result+=count+',';}//重置为0count =0;}console.log(result);}</script><body>请输入句子:<input type="text" id="worldText" /><input type="button" onclick="countWorld()" value="统计单词"/></body></html>


0 0