Skip to content

Instantly share code, notes, and snippets.

@masautt
Created September 6, 2019 16:14
Show Gist options
  • Save masautt/1109907cf23eaae3d9e981f190ff938d to your computer and use it in GitHub Desktop.
Save masautt/1109907cf23eaae3d9e981f190ff938d to your computer and use it in GitHub Desktop.
How to get number of vowels and consonants in JavaScript?
function getLetters(words) {
var words = (typeof words == 'string') ? words : '',
count = re => (words.match(re) || []).length,
vowels = count(/[aeiou]/ig),
consonants = count(/[bcdfghjklmnpqrstvxzwy]/ig);
return {vowels, consonants};
}
console.log(getLetters("Marek Sautter")); // --> { vowels: 5, consonants: 7 }
// https://codereview.stackexchange.com/questions/128121/count-the-number-of-vowels-and-consonants
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment