Let's say I've got the following string:
"dog cat hello cat dog dog hello cat world"
and the two words "hello" and "world".
I want to get the string that is between these words and where the two words are closest (in terms of number of words between them) to each other. In this example the following strings would be between these two words:
- "cat dog dog hello cat"
- "cat"
Since "hello" and "world" are the closest in the 2nd option, the desired result would be "cat" in this example.
How do I do this in regEx (in JS flavor)?
The best I could come up is
(?<=hello\s+).*?(?=\s+world)
but that only gives me the 1st option, i.e. "cat dog dog hello cat"