I’m trying to write a regex word count, but most of the examples I’ve found only cover a portion of the following situations:
- hello,there = should be 2 words (note there is no space after the
comma) - hello , there = should be 2 words (note the space before and
after the comma) - $10,000 = should be 1 word
- hello there ? = should be 2 words (note the space before the
question mark) - hello-there = should be 2 words
- http://www.google.com = should be 1 word
- http://www.google.com/analytics = should be 1 word
I’m currently using the following code in jquery
var total_words = $.trim($("#mytextbox").value).split(/[\s\-\.\\\/\?\!]+/).length
but it only covers situations #3 and #5.
Covering URLs is less of a priority to me.
Any help would be appreciated!
The following RegEx will work for all your examples:
This RegEx was converted from JSoft to JavaScript:
This first section defines any url type item:
The section section is for currency with or without decimals:
Finally the third section matches words: