Dan Schnau

Use A RegEx to get the Word Count out of a string in JavaScript

I added a 'word count' feature to my blog editor this morning. This is the regular expression to match spaces and line breaks:

/\s/

And in JavaScript:

const count = mystring.split(/\s+/).length;