1. Home
  2. Computing & Technology
  3. JavaScript
photo of Stephen Chapman
Stephen's JavaScript Blog

By Stephen Chapman, About.com Guide to JavaScript since 2004

Counting Words

Thursday August 7, 2008
Count words with JavaScript

Have a text area in a form and want to know how many words your visitor enters? This simple JavaScript will keep count for you.

Counting Words
Comments
July 25, 2006 at 8:06 pm
(1) Robert says:

I had to change:
a=y.replace(’\n’,’ ‘);
to:
a=y.replace(/\n/g,’ ‘);

otherwise words separated only by newlines weren’t counted. (firefox v1.5)

July 28, 2006 at 5:05 pm
(2) javascript says:

I have updated the page to use /\s/g so that it will even allow for multiple spaces or new lines between words without messing up the count.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.