devisrinerusu_words_utils
Version:
The Words_Utils Module offers a simple function to analyze text, including counting vowels,consonants,words,number of occurence of words
29 lines (24 loc) • 735 B
JavaScript
function countVowels(text) {
const matches = text.match(/[aeiouAEIOU]/g);
return matches ? matches.length : 0;
}
function countConsonants(text) {
const matches = text.match(/[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]/g);
return matches ? matches.length : 0;
}
function countWords(text) {
const words = text.trim().split(/\s+/);
return words.length;
}
function countWordOccurrences(text, word) {
if (!word) return 0;
const words = text.toLowerCase().match(/\b\w+\b/g) || [];
const matches = words.filter(w => w === word.toLowerCase());
return matches.length;
}
module.exports = {
countVowels,
countConsonants ,
countWords,
countWordOccurrences
}