I need to extract the string between the first occurrences of given word and dot (.).
The following code works fine and extracts the string between the first occurrence of the word 'located' and the first occurrence of '.'. In this case, it produces the answer: "in Canada".
var myStr = "Toronto is located in Canada. And located in USA.";
var matches = myStr.match(/\located(.*?)\./);
var str = matches && matches.length ? matches[1] : '';
alert(str);
But I need to be able to supply given word as a variable and insert it into String.Match regex.
I've tried this:
var myStr = "Toronto is located in Canada. And located in USA.";
var test = "located";
var regex = new RegExp("/\"" + test + "(.*?)\./");
var matches = myStr.match(regex);
var str = matches && matches.length ? matches[1] : '';
alert(str);
But it doesn't work. What am I doing wrong?