death the kid
death the kid

Reputation: 33

word count method

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;
        }
    }
}

Upvotes: 0

Views: 1838

Answers (7)

Keith
Keith

Reputation: 155652

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|']+).

Upvotes: 0

Jaanus
Jaanus

Reputation: 17866

Given 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.

Upvotes: 5

richardtallent
richardtallent

Reputation: 35363

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:

  1. Slightly smaller overhead when the string has many duplicated whitespace characters.
  2. 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)

Upvotes: 5

Galen
Galen

Reputation: 30170

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);

Upvotes: 1

Ondrej Slint&#225;k
Ondrej Slint&#225;k

Reputation: 31910

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

Upvotes: 0

prime_number
prime_number

Reputation: 758

x = "a b f d"; alert('x has ' + x.split(/\s+/).length + ' words')

Upvotes: 0

Paul Sasik
Paul Sasik

Reputation: 81429

A simpler method would be to use JavaScript RegEx to count the words.

Upvotes: 0

Related Questions