JavaScript Tutorial

Convert Sentence to Array of Words

Function that splits a sentence into an array of words, handling punctuation.

function sentenceToArray(str) {
  return str.match(/[\w'-]+/g);
}
console.log(sentenceToArray('Hello, world!')); // ['Hello', 'world']