Advertisement
Guest User

Word count function

a guest
Apr 19th, 2013
1,170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. var countre = /[\w\u2019\x27\-]+/g,
  2. cleanre = /[0-9.(),;:!?%#$?\x27\x22_+=\\\/\-]*/g;
  3.  
  4. function wordCount(tx)
  5. {
  6. var tc = 0;
  7.  
  8. if (tx) {
  9. tx = tx.replace(/\.\.\./g, ' '); // convert ellipses to spaces
  10. tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' '); // remove html tags and space chars
  11.  
  12. // deal with html entities
  13. tx = tx.replace(/(\w+)(&.+?;)+(\w+)/, "$1$3").replace(/&.+?;/g, ' ');
  14. tx = tx.replace(cleanre, ''); // remove numbers and punctuation
  15.  
  16. var wordArray = tx.match(countre);
  17. if (wordArray) {
  18. tc = wordArray.length;
  19. }
  20. }
  21.  
  22. return tc;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement