Skip to main content

How to count occurrences of words, characters, sentences using Regular Expression

Here is a Regular Expression regex to count words, to count blank spaces, to count sentences, to count paragraphs in Java script.

Regular Expression to count words in Text.

text.match(/(\w+)/g).length;

Regular Expression to count paragraphs in text.

text.replace(/\n$/gm, '').split(/\n/).length;

Regular Expression to count Sentences in Text.

text.split(/[\.!\?]+\s/g).filter(Boolean).length;

Regular Expression to count spaces in Text.

text.split(" ").length - 1;

Use this Word Character Counter Online Tool. Another Tool is used to capitalize the first character of every word.