Split a sentence on question(?) mark and dot(.) and count the total words? Node js

In NodeJs how to split the sentence on question(?) mark and dot(.) and count the total words?

e.g sentence = “i am a boy. who are you?”

expected result in 3 sentence 
1.  i am a boy
2.  who are you
3. ''


1 sentence has 4 words
2 sentence has 3 words
3 sentence empty

Answer

const string = "i am a boy. who are you?";
const sentences = string.split(/[.?]+/);

const counts = sentences.map((str, index) => {
  const count = str ? str.trim().split(' ').length : 0;
  const returnVal = count ? `${index+1} sentence has ${count} words` : `${index+1} sentence empty`;
  return returnVal;
});

console.log(counts);