0

Alright, so I have this script here jsfiddle.net/CDLtn/2/
that counts up the words and displays a value based on if any of the checkboxes were checked it works fine except one thing, it doesen't work with Russian input.

$(function () {
var wordCounts = {};
$("input[type='text']:not(:disabled)").keyup(function () {
var matches = this.value.match(/\b/g);
wordCounts[this.id] = matches ? matches.length / 2 : 0;
var finalCount = 0;
var x = 0;
$('input:checkbox:checked').each(function () {
    x += parseInt(this.value);
});
x = (x == 0) ? 1 : x;
$.each(wordCounts, function (k, v) {
    finalCount += v * x;
});
$('#finalcount').val(finalCount)
}).keyup();
$('input:checkbox').change(function () {
$('input[type="text"]:not(:disabled)').trigger('keyup');
});
});

I've found an open source counter http://roshanbh.com.np/2008/10/jquery-plugin-word-counter-textarea.html and this one does accept Russian input ( here is a fiddle of the link above jsfiddle.net/Joniniko/TyPSJ/ )

I need to either somehow make my original counter work with Russian input, or maybe incorporate the checkbox feature into the one that was made by Roshan.

Here is an example of russian text just incase "Привет как дела"

(My source page encoding has been changed to UTF-8 already, and ive also tried other ones for cyrillic input)

UPD: jsfiddle.net/Joniniko/CDLtn/5/ this accepts the russian input, but it increments the counter by 0.5 instead of 1 for some unknown to me reason

Konata
  • 275
  • 1
  • 3
  • 14
  • possible duplicate of [Russian input for word count](http://stackoverflow.com/questions/18551061/russian-input-for-word-count) – bobs12 Aug 31 '13 at 20:11

1 Answers1

1

The problem is the way you have defined "words". The \b regex escape sequence only recognizes [a-zA-Z0-9] as "word characters".

> "Привет как дела".match(/\b/g);
null

I think what you want to do instead is split your words along spaces:

> "Привет как дела".split(/\s+/);
["Привет", "как", "дела"]
slashingweapon
  • 11,007
  • 4
  • 31
  • 50
  • Yes, thats in the case if i only needed that specific line, but i need it to be able to count any random russian word, i tried the split http://jsfiddle.net/Joniniko/CDLtn/4/ It started to count it wierdly by 0.5 – Konata Aug 31 '13 at 20:00
  • Question already asked and answered here: http://stackoverflow.com/questions/18551061/russian-input-for-word-count/ – bobs12 Aug 31 '13 at 20:12
  • 1
    I forked and fixed your script at http://jsfiddle.net/slashingweapon/7JmGn/ - you needed to trim the string before splitting it, and then not divide the length by two. – slashingweapon Aug 31 '13 at 20:13