想了很久,卡在了不知道找出重复单词及个数,我放到数组里,输出计数都不太正确
采用类似冒泡法的算法 或者列表发从首字母A开始查
var s = 'During the ITB China 2018 held in Shanghai, COTRI presented the CTW Award 2018 in the Special Individual Recognition category to Mr. David L. Shields, Vice President, Sales of Island Routes Caribbean Adventures. CTW Award is recognized inside and outside China as the most important quality sign for the international Chinese outbound market.'
var kv = {}, arr = s.split(/[ , .]/g)
for (var i = 0; i < arr.length; i++)
if (arr[i]) {
kv[arr[i]] = (kv[arr[i]] || 0) + 1
//如果不考虑大小写用下面的
//kv[arr[i].toLowerCase()] = (kv[arr[i].toLowerCase()] || 0) + 1
}
alert(JSON.stringify(kv, null, 4))
//只考虑重复的可以删除删除个数为1的
for (attr in kv) if (kv[attr] == 1) delete kv[attr]
alert(JSON.stringify(kv, null, 4))
//替换in ==> 在
var kw = 'in', to = '在'
s = s.replace(new RegExp('\\b' + kw + '\\b', 'gi'), to);
alert(s)
js些有困难,可以到后台处理不
这个用正则表达式来实现是最合适的
对数组进行双层for循环比对,计数不正确可能是拆分字符串的时候有问题
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title> 页面名称 </title>
</head>
<body>
<textarea id="text" rows="10" cols="40">
JavaScript (JS) is a lightweight interpreted or JIT-compiled programming language with first-class functions. While it is most well-known as the scripting language for Web pages, many non-browser environments also use it, such as Node.js, Apache CouchDB and Adobe Acrobat. JavaScript is a prototype-based, multi-paradigm, dynamic language, supporting object-oriented, imperative, and declarative (e.g. functional programming) styles. Read more about JavaScript.
</textarea><br />
<input type="button" value="统计" onclick="word();" /><br />
查找:<input type="text" id="t1" value="js" /><br />
替换:<input type="text" id="t2" value="es" /><br />
<input type="button" value="替换" onclick="replace();" />
<script type="text/javascript">
var text = document.getElementById("text");
function word() {
var arr = text.value.match(/[\w\-]+/g) || [];
var str = "单词个数:"+arr.length+"\n";//,并找出来重复的单词,并指明个数,并";
var k = {}, p = {};
for (var i = 0; i < arr.length; i++) {
var v = arr[i].toLowerCase();
if (p[v]) {
if (k[v]) {
k[v]++;
} else {
k[v] = 2;
}
} else {
p[v] = 1;
}
}
str += "重复的单词及个数"+JSON.stringify(k,null,4);
alert(str);
}
function replace() {
var t1 = document.getElementById("t1").value;
var t2 = document.getElementById("t2").value;
text.value = text.value.replace(new RegExp("\\b"+t1+"\\b","ig"),t2);
}
</script>
</body>
</html>
let article = 'In my dual profession as an educator and health care provider, I have worked with numerous children infected with the virus that causes AIDS. The relationships that I have had with these special kids have been gifts in my life. They have taught me so many things, but I have especially learned that great courage can be found in the smallest of packages. Let me tell you about Tyler.'
let arry = article.split(' ')
const words = arry.length // 单词个数
arry = arry.map(word => word.replace(/\,|\.|\"|\'|\!/g, '')) // 替换单词中的符号
// 统计重复单词个数
const wordObj = {}
let count = 0
for (let i = 0, len = arry.length; i < len; i++) {
// 如果这个单词已经存在则跳过
if (wordObj[arry[i]]) {
continue
}
for (let j = 0; j < len; j++) {
if (arry[i] === arry[j]) {
count++
}
}
wordObj[arry[i]] = count
count = 0
}
// 这样就可以看出每一个单词的个数了
console.log(wordObj)
// 替换单词 把I替换成小写i
article = article.replace(/\sI\s/g, ' i ')
console.log(article)
var article = 'During the ITB China 2018 held in Shanghai, COTRI presented the CTW Award 2018 in the ' +
'Special Individual Recognition category to Mr. David L. Shields, Vice President, Sales of Island' +
' Routes Caribbean Adventures. CTW Award is recognized inside and outside China as the most important ' +
'quality sign for the international Chinese outbound market.';
var words = article.split(' ');
var regexp = /\w*/; // 正则表达式, 找到所有大小写字母与数字
// 把每个单词标点符号去掉
var wordsArr = words.map(function (item) {
return item.match(regexp)[0]
});
console.log(wordsArr.length); // 统计单词个数
var uniqueArr = {};
// 去重
wordsArr.map(function (item) {
// 找到item 存在于 uniqueArr的
if (item in uniqueArr) {
uniqueArr[item].count++;
} else {
uniqueArr[item] = {
key: item,
count: 1
}
}
});
// 将每个词对应出现的个数标记出来
// console.log(uniqueArr);
/* str 你要找的单词 , arr 单词数组 ,
* idx 替换的单词的序列(比如the 出现了5次, idx为5时 替换第五个 the)
* repStr 你要替换的单词
*/
function replace(str, arr, idx, repStr) {
// 找到单词在文章中的所有位置
var indexArr = getIndex(str, arr);
// 替换单词
arr[indexArr[idx]] = repStr;
}
// 获取某个元素在当前数组中的所有位置
function getIndex(ele, Arr) {
var arr = [];
Arr.map(function (item, index) {
if (item == ele) {
arr.push(index)
}
});
return arr;
}
注释已经写清楚了, 希望能帮到