Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for javascript to count words (excluding numbers)

Tags:

regex

I have this function

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

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

like image 210
Useer Avatar asked Jun 17 '26 08:06

Useer


1 Answers

The following regex might help you out:

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

The ^ negates the characters in the brackets, so all characters are allowed to follow the whitespaces except any numbers.

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

like image 156
Jo Oko Avatar answered Jun 20 '26 16:06

Jo Oko