Regex for javascript for word count (excluding numbers)

I have this function

String.prototype.countWords = function(){
    return this.split(/\s+\b/).length;
}

      

which counts words in textarea, but also counts the numbers inserted, I was wondering how to count words but not numbers, so ignoring numbers,

+3


source to share


2 answers


The following regex might help you:

String.prototype.countWords = function(){
    return this.split(/\s+[^0-9]/).length;
}

      



^

cancels the characters in parentheses, so all characters are allowed to follow spaces except for any numbers.

By the way: here's a good place to test your regex: http://regexpal.com/

+1


source


Your regex is counting the number of spaces. Use the following words (no numbers):

/[a-zA-Z]+/

      



Use the following for separation:

this.split(/[\s\D]+/).length

      

+1


source







All Articles