Number of Valid Words in a Sentence LeetCode solution in JavaScript
1/**2* @param {string} sentence3* @return {number}4*/5var countValidWords = function (sentence) {6let list = sentence.split(" ");7let filtered = list.filter((s) => {8if (/\d/.test(s) || s === "") return false; //removes anything with numbers or is blank9if (/^[!,.]$/.test(s)) return true; //punctuation only10if (/^\w+[!,.]?$/.test(s)) return true; //word + optional punctuation11if (/^\w+[-]?\w+[!,.]?$/.test(s)) return true; //word + optional hypen + word + optional punctuation12return false;13});1415return filtered.length;16};