I'm trying to turn this:
"This is a test this is a test"
into this:
["This is a", "test this is", "a test"]
I tried this:
const re = /\b[\w']+(?:[^\w\n]+[\w']+){0,2}\b/
const wordList = sample.split(re)
console.log(wordList)
But I got this:
[ '',
' ',
' ']
Why is this?
(The rule is to split the string every N words.)
.split()doesn't include the delimiter so it does the opposite of what you want. You need to do a regular regex search (with agmodifier) instead of split.