使用日语的js计算textarea中的单词

时间:2014-08-11 07:18:47

标签: javascript regex

我正在尝试使用JS来计算textarea中的单词,它适用于英语,但它会返回错误的日语单词计数。任何人都可以帮助我吗?感谢

function word_count(string)
{
  r1 = new RegExp('[\u3000-\u4DFF]', 'g');
  r2 = new RegExp('[\u4E00-\u9FFF]', 'g');
  string = string.replace(r1, ' {PNK} ');
  string = string.replace(r2, ' {CJK} ');
  //string = string.replace(/(<([^>]+)>)/ig,"") ;
  string = string.replace(/(\(|\)|\*|\||\+|\"|\'|_|;|:|,|\.|\?)/ig, " ");
  string = string.replace(/\s+/ig, " ");
  //string = string.replace(/_+/ig," ");
  var a = string.split(/[\s+|\\|\/]/g);

  var count = 0;
  var pnkCounter = 1;
  for (var i = 0; i < a.length; i++) {
    if (a[i] == '{PNK}' && pnkCounter % 3 == 0) {
      count++;
      pnkCounter++;
    } else if (a[i] == '{PNK}') {
      pnkCounter++;
    } else if (a[i].length > 0) {
      count++;
    }
  }
}

0 个答案:

没有答案