这不是完美的,但作品,这里是一个演示:
(此演示只是发现常用字词)
- 它分裂与正则表达式的文本
- 然后计数单词
- 然后返回最频繁的单词
var data = document.getElementById("data").value;
var allWords = data.split(/\b/);
var wordCountList = {};
allWords.forEach(function(word){
if(word !== " "){
if(!wordCountList.hasOwnProperty(word)){
wordCountList[word] = {word: word, count:0};
}
wordCountList[word].count++;
}
})
var maxCountWord = {count:0};
for(var propName in wordCountList){
var currentWord = wordCountList[propName];
if(maxCountWord.count<currentWord.count){
maxCountWord = currentWord;
}
}
console.info(maxCountWord);
textarea{
width:100%;
height:100px;
}
<textarea id="data" >
<!-- start slipsum code -->
The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.
<!-- end slipsum code -->
</textarea>
<div id="result"></div>
要更换您还可以使用正则表达式的话:
(这只是演示代替了常用字词)
function freewordchoice (free, word, choice){
var data = document.getElementById("data").innerHTML;
var replaceExpression = new RegExp("\\b"+word+"\\b","gi");
console.info(replaceExpression);
data =data.replace(replaceExpression, free + word + choice);
document.getElementById("result").innerHTML = data;
}
freewordchoice("<b>", "the", "</b>");
<b>Before:</b>
<div id="data" >
<!-- start slipsum code -->
The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.
<!-- end slipsum code -->
</div>
<br/><br/>
<b>After:</b>
<div id="result" >
</div>
更新:
问题是此行
common = 'the,a,do,in,with,this,so,that,of,and,not,did,when,what,were,went,was,as,
if,who,had,at,can,you,which,while,will,to,till,then,them,their,she,
he,once,out,no,must,many,me,is,it,his,him,her,about,have,i,has,your,
would,where,whom,s,on,from,for,by,but,all,said,my,';
的问题是在串,said,my,';
的最后删除最后一个逗号,它应该工作,像这样:
common = 'the,a,do,in,with,this,so,that,of,and,not,did,when,what,were,went,was,as,
if,who,had,at,can,you,which,while,will,to,till,then,them,their,she,
he,once,out,no,must,many,me,is,it,his,him,her,about,have,i,has,your,
would,where,whom,s,on,from,for,by,but,all,said,my';
由于通过最后一个逗号,最后一个字是空字符串。
你有一些测试数据吗?文本文件将如何显示?什么编码?哪些语言特殊字符等? –
我更新了我的答案,以涵盖您的问题的替换部分。 –
这是我正在从事的演示的地方:http://internetstall.nu/demo/demo.html,我只是上传一个简单的包含文本的.txt文件。不知道你是什么意思的特殊字符,我试图用JavaScript来完成,但有些事情告诉我,这不是你的意思。已经解决了问题 – user3481279