本文介绍了字数统计框javascript/php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于最合适的匹配项"一词,如果用户键入缩写形式为""MSM" ",则MSM也应计为3个单词.
以同样的方式,如果有大学名称,例如"DAV",则也应计为3个单词.
for word "Most Suitable Match" if user type short form as "MSM" then MSM also shall be counted as 3 words.
In the same way if there is name of college like "DAV" then it shall also be counted as 3 words.
我正在尝试在文本框中实现单词计数器.我正在使用以下链接:
I am trying to implement a word counter in textbox. I am using the links below:
JS小提琴 第二个链接
<textarea name="myMessage" onkeyup="wordcount(this.value)"></textarea> <script type=""text/javascript""> var cnt; function wordcount(count) { var words = count.split(/\s/); cnt = words.length; var ele = document.getElementById('w_count'); ele.value = cnt; } document.write("<input type=text id=w_count size=4 readonly>"); </script>文字计数器工作正常.但是我的情况如下:
Word counter is working fine. But my scenario is like as below:
请提出建议!
推荐答案我有一个简单的功能:
var regex = [/DAV/g, /MAC/g]; function countWords() { var count = []; regex.forEach(function(reg) { var m = text.match(reg); if (m) { count = count.concat(m); } }); // the number of known acronym wrote in the text var acronyms = count.length; // how much words generated from an acronym (e.g. DAV === 3 words; AB === 2 words and so on) var wordsFromAcronyms = count.join().replace(/,/g,'').length; // how many words wrote (this is equal to your code) var rawWords = text.match(/\S+/g).length; // compute the real number return rawWords - acronyms + wordsFromAcronyms; }计算首字母缩写词的数量(已知首字母缩写词的列表存储在regex数组中),然后计算首字母缩写词(wordsFromAcronym)生成多少个单词,然后减去首字母缩写词的数量(rawWords)中的(acronyms),然后加上wordsFromAcronym.
It counts the number of the wrote acronym (the list of the known acronyms is stored in regex array), then count how much words are generated by the acronyms (wordsFromAcronym), and then substract the number of acronyms (acronyms) from the total words (rawWords) and then add the wordsFromAcronym.
这里是 PLNKR .
更多推荐
字数统计框javascript/php
发布评论