Build a word counter:
function analyzeText(text) {
let trimmed = text.trim()
let charCount = trimmed.length
let wordCount = trimmed.split(/\s+/).filter(w => w.length > 0).length
let sentenceCount = trimmed.split(/[.!?]+/).filter(s => s.length > 0).length
return {
characters: charCount,
words: wordCount,
sentences: sentenceCount
}
}
let stats = analyzeText("Hello world. How are you?")
console.log(stats) // { characters: 25, words: 5, sentences: 2 }