字数统计框javascript/php [英] word count textbox javascript/php

查看:71
本文介绍了字数统计框javascript/php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在文本框中实现单词计数器.我正在使用以下链接:

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:

  1. 对于最合适的匹配项"一词,如果用户键入缩写形式为""MSM" ",则MSM也应计为3个单词.
  2. 以同样的方式,如果有大学名称,例如"DAV",则也应计为3个单词.
  1. for word "Most Suitable Match" if user type short form as "MSM" then MSM also shall be counted as 3 words.
  2. In the same way if there is name of college like "DAV" then it shall also be counted as 3 words.

请提出建议!

推荐答案

我有一个简单的功能:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆