Number of Valid Words in a Sentence

EasyString

Solution

export function countValidWords(sentence: string): number {
  const words = sentence.split(/\s+/);
  function isValid(word: string): boolean {
    return 0 < word.length && /^(?:[a-z]+(?:-[a-z]+)?)?[!.,]?$/.test(word);
  }
  return words.filter(isValid).length;
}