How can I count words with comas, spaces or both?

  • Profile Image
    Asked on February 18, 2012 at 12:21 PM

    I want to use a text area and count each word typed, but your currently script (... if(limitType=='Words'){count=$A(input.value.split(/\s+/)).without("").length; ...) can only counting words separated by an spaces, (Ex: oneword twowords   2/2) but in case of those 'bad' users, they could use a 'trick' (Ex: oneword,twowords    1/2) so, I would like to know how can I validate not only spaces, even comas, dots, or mixed with spaces, like ' ,', to count real words in a sentence.


    I tried changing your script (... if(limitType=='Words'){count=$A(input.value.split(/([',']|[\s+])/)).without("").length; ...) but now I'm counting a coma or space as a word.


    I'll really apreciate your help, thanks.

  • Profile Image
    Answered on February 18, 2012 at 01:42 PM

    Solved! by myself but nevermind, in case someone else need it. count=$A(input.value.split(/,|\s+/)).without("").length;


    This way you'll count words by coma and space :)