0

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

YASH KUMAR
  • 35
  • 4

2 Answers2

1
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' ]
Dan P.
  • 1,707
  • 4
  • 29
  • 57
1

I think you can try this:

const str = 'I want to deliver goods from Indonesia to India';
str.match(/(?<=to\s+)(\w+)/g)
thang
  • 323
  • 4
  • 7