I want to find the text occurring between the last occurrence of the word "to" in a sentence and another word "Mary".
So, for the sentence
"I went on Friday morning to bed, then to arrive for that thing and then to beloved Mary"
My regex should match the string "beloved".
I've tried the following code:
var str = "I went on Friday morning to bed, then to arrive for that
thing and then to beloved Mary";
var r = /to\b(.*)Mary/g
var match = r.exec(str);
console.log(match);
And am using the global flag as I want to match all occurrences of the word "to". I'm expecting it to return an array of strings, with one of them being the string "beloved" but the output I get is:
["to bed, then to arrive for that thing and then to beloved Mary", " bed, then to arrive for that thing and then to beloved "]
Jsbin here:
http://jsbin.com/puyijo/edit?js,console
Any suggestions?
EDIT: The word "Mary" is not necessarily at the very end of the string.
Maryat end of string ?/.*to\b *(.*?) *\bMary/g