This is my code
var words = input.value.match(/\s[\w\u0402-\u045f]+/ig);
if (words) {
wordCount.innerHTML = words.length;
} else {
wordCount.innerHTML = 0;
}
This is my code
var words = input.value.match(/\s[\w\u0402-\u045f]+/ig);
if (words) {
wordCount.innerHTML = words.length;
} else {
wordCount.innerHTML = 0;
}
For further actions, you may consider blocking this person and/or reporting abuse
Dharmen Shah -
Nik L. -
Maxim Saplin -
Ian Ndeda -
Top comments (8)
I think your regex is excluding the first word.
I chenged the example in developer.mozilla.org/en-US/docs/W...
Can you share what are you trying to do with this code?
thepracticaldev.s3.amazonaws.com/i...
I need accurate counting. Code didn't count first word.
I think the regex expression is not correct, for some reason. Alternatively, you can try to split the string into an array, and loop each element. Not sure about performance in this case
It is correct. Counting Cyrillic words. But it starts counting from 0 not from 1.
check the code i posted above. It has 2 words, but if you print found, it shows only the second word. The length is 1.
So, it starts counting on 1, but for some reason, your regex is skipping the first word. If you have only one word in the paragraph variable, the result is null.
You are checking if space exists before word. Since first word doesn't have space before it, it fails to count it.
Try this, it includes beginning of the text or white space check.