2026-03-17•3 min read•by DevUtilz
Word Counter and Text Analysis Tools
Word CountTextAnalysisTools
Word Counter and Text Analysis Tools
Word counters are essential for writing, SEO, and content analysis.
Basic Word Count
function countWords(text) {
return text
.trim()
.split(/\s+/)
.filter(word => word.length > 0)
.length;
}
// Usage
countWords('Hello world'); // 2
countWords(' Hello world '); // 2
Character Count
function countChars(text) {
return text.length;
}
function countCharsNoSpaces(text) {
return text.replace(/\s/g, '').length;
}
Line Count
function countLines(text) {
return text.split('\n').length;
}
function countNonEmptyLines(text) {
return text
.split('\n')
.filter(line => line.trim().length > 0)
.length;
}
Complete Analysis
function analyzeText(text) {
const words = text.trim().split(/\s+/).filter(w => w.length > 0);
return {
characters: text.length,
charactersNoSpaces: text.replace(/\s/g, '').length,
words: words.length,
sentences: (text.match(/[.!?]+/g) || []).length,
paragraphs: text.split(/\n\s*\n/).filter(p => p.trim().length > 0).length,
lines: text.split('\n').length,
readingTime: Math.ceil(words.length / 200), // 200 words/min
uniqueWords: [...new Set(words.map(w => w.toLowerCase()))].length
};
}
Reading Time Calculation
function getReadingTime(text, wordsPerMinute = 200) {
const words = text.trim().split(/\s+/).length;
const minutes = Math.ceil(words / wordsPerMinute);
if (minutes < 1) return 'Less than a minute';
if (minutes === 1) return '1 minute';
return `${minutes} minutes`;
}
Word Frequency
function getWordFrequency(text, limit = 10) {
const words = text.toLowerCase()
.replace(/[^\w\s]/g, '')
.split(/\s+/)
.filter(w => w.length > 2);
const freq = {};
words.forEach(word => {
freq[word] = (freq[word] || 0) + 1;
});
return Object.entries(freq)
.sort((a, b) => b[1] - a[1])
.slice(0, limit);
}
Common Use Cases
1. Blog Post SEO
function checkSEOWords(text) {
const count = countWords(text);
const min = 300;
const ideal = 1500;
return {
count,
passed: count >= min,
recommendation: count < min
? `Add ${min - count} more words`
: count >= ideal
? 'Good length for SEO'
: `Consider adding ${ideal - count} more words`
};
}
2. Social Media Limits
const limits = {
twitter: 280,
linkedin: 3000,
instagram: 2200
};
function checkLimit(text, platform) {
const limit = limits[platform];
return {
remaining: limit - text.length,
exceeded: text.length > limit
};
}
3. Document Requirements
function meetsRequirements(text, requirements) {
const analysis = analyzeText(text);
const results = {};
for (const [key, min] of Object.entries(requirements)) {
results[key] = {
current: analysis[key],
required: min,
passed: analysis[key] >= min
};
}
return results;
}
Best Practices
- Trim whitespace - Before counting
- Handle special chars - Clean punctuation
- Consistent splitting - Use regex for split
- Handle edge cases - Empty text, just spaces
Conclusion
Use word counters for writing, SEO, and content requirements. Most platforms have built-in limits. Reading time is useful for user experience.