Analyse any text
Word counters, keyword tools, spam filters — they all start the same way: split text into words, count them, find the most common. You'll combine regex, Map, Set and sorting.
From text to tokens
This is a small version of how search engines and spam filters begin.
Every text tool starts by turning a block of text into tokens — the individual words you'll count. Splitting on spaces isn't enough: punctuation sticks to words ("momo," vs "momo"), capitals make the same word look different ("Momo" vs "momo"), and apostrophes belong inside words ("don't"). A regex that matches runs of letters and apostrophes, plus lower-casing, handles all three.
const text = "Momo, momo and MORE momo! Don't stop."; const words = (text.match(/[a-z']+/gi) ?? []).map((w) => w.toLowerCase()); console.log(words); console.log(words.length, new Set(words).size);
That ?? [] matters: when there's no match, match returns null, not an empty array. Calling .map on null would crash. Treat "no words" as a normal input that should give zero counts, not an error.