I want to find all the words that occur after a particular word. For eg:
I want to deliver goods from Indonesia to India
Here, I want all the words that occur after to ,so the result should be an array containing deliver and India
I want to find all the words that occur after a particular word. For eg:
I want to deliver goods from Indonesia to India
Here, I want all the words that occur after to ,so the result should be an array containing deliver and India
let str = 'I want to deliver goods from Indonesia to India to';
let arr = str.split(' ');
let words_following_to = [];
arr.map((word, i) => {
if (word.toLowerCase() === 'to' && arr[i + 1]) {
words_following_to.push(arr[i + 1]);
}
});
console.log(words_following_to); // [ 'deliver', 'India' ]
I think you can try this:
const str = 'I want to deliver goods from Indonesia to India';
str.match(/(?<=to\s+)(\w+)/g)