IT & Programming

Count number of words with JQuery

This simple and easy JQuery function shows total number of words in a given string, text or paragraph.

Script

var text = "This is an example text";

var words = text.replace(/^[\s,.;]+/, "")
                .replace(/[\s,.;]+$/, "")
                .split(/[\s,.;]+/)
                .length;

alert(words);

Output

5

View Demo

Leave A comment

Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *