I have recently used modified a word count method in javascript for my website, so that it counts the intial amount words in the textarea, but it doesn't quite work
function wordCounter(field,countfield)
{
var maxlimit = 200;
var wordcounter = maxlimit - information.value.split(' ').length;
for (x = 0; x < field.value.length; x++)
{
if(field.value.charAt(x) == " " && field.value.charAt(x-1) != " ") // Counts the spaces while ignoring double spaces, usually one in between each word.
{
wordcounter++
}
if (wordcounter > 250)
{
field.value = field.value.substring(0, x);
}
else
{
countfield.value = maxlimit - wordcounter;
}
}
}
I have recently used modified a word count method in javascript for my website, so that it counts the intial amount words in the textarea, but it doesn't quite work
function wordCounter(field,countfield)
{
var maxlimit = 200;
var wordcounter = maxlimit - information.value.split(' ').length;
for (x = 0; x < field.value.length; x++)
{
if(field.value.charAt(x) == " " && field.value.charAt(x-1) != " ") // Counts the spaces while ignoring double spaces, usually one in between each word.
{
wordcounter++
}
if (wordcounter > 250)
{
field.value = field.value.substring(0, x);
}
else
{
countfield.value = maxlimit - wordcounter;
}
}
}
Share
Improve this question
edited Nov 17, 2009 at 19:14
Robert Cartaino
28.2k6 gold badges46 silver badges67 bronze badges
asked Nov 17, 2009 at 19:07
death the kid death the kid
336 bronze badges
7 Answers
Reset to default 5Given string "s", you do this:
var numWords = s.replace(/^\s+|\s+$/g,"").split(/\s+/).length;
This splits the string at all whitespaces (spaces, linebreaks etc), also working with multiple spaces etc. EDIT: added inline trim to strip whitespace from beginning/end.
The easiest solution is to count the number of non-consecutive whitespace characters (spaces, tabs, etc.), plus one.
RegEx:
\S\s
JavaScript:
var str = "The fox jumped over the lazy dog.";
var wordcount = str.match(/\S\s/g).length + 1;
Note that I didn't use "\s+", because I don't need to match all whitespace, only the whitespace characters that occur after a non-whitespace. This has two advantages:
- Slightly smaller overhead when the string has many duplicated whitespace characters.
- Won't return an extra word in the count if the input starts with whitespace.
Many answers here use split() instead. The only advantage to split() is not having to add 1 to the answer, but IMHO, match() is the better answer because the code is more readable. The purpose of the code is to find word boundaries, not to split the words.
Also, though match() and split() return arrays, match() has a smaller memory overhead, for two reasons:
- One fewer element (not a big deal)
- It only returns two characters in each array element (could be significant)
The simple way would be to count the number of spaces and add 1.
Edit: add example
This essentially does that. Splitting by the spaces
var str = 'adfs asdf a asdf';
alert(str.split(/\s+/).length);
A simpler method would be to use JavaScript RegEx to count the words.
x = "a b f d"; alert('x has ' + x.split(/\s+/).length + ' words')
I'm not sure if I understand what exactly do you need (the code you pasted confuses me a bit), but here's a simple function for word count:
var text = "1 2 3 4000";
function wordCounter( text ) {
word_count = text.split(" ");
return word_count.length;
}
wordCounter( text ); // returns 4 as expected
There is a word break match in Regex that lets you search for anything that is the boundary of a word, not just spaces (\s
) but punctuation too.
So:
const wordCount =
'the quick,brown fox jumped over the lazy dog.'.match(/\b\w+\b/gi).length + 1
However, this also breaks on '
, which counts as a boundary too, so if you want to handle possesive apostrophes you need to account for them too, either by stripping them, or by incuding them in the word match (something like [\w|']+
).