How to chunk a string in to 3-word(or less) phrases with brackets at ends?
Following is a sample string
This is a sample sentence containing some words with some other meanings.
Following is the expected result
[This is a sample] [sentence containing some] [words with some] [other meanings.]
I've added mine
this is what i've tried
this.mod = this.data.replace(/([.?!])\s*(?=[A-Z])/g, "$1|").split("|");
this.mod.map((sentence) => {
sentence.split(' ').reduce((acc, cur, idx, arr) => {
acc + cur
} ,'')
})
This is also what I've tried & it is not working.
const res = this.mod.map((sentence) => {
return sentence.split(" ").reduce((acc, cur, idx, arr) => {
acc + (idx % 3 === 0) ? `][${cur}]` : cur;
}, "[");
});
Is there any other approach?