How do I get a string between two strings using match with variables? The following code works well if I use match with strings Regular Expression to get a string between two strings in Javascript I also tried to apply the info at JavaScript - Use variable in string match :
var test = "My cow always gives milk";
var testRE = test.match("cow(.*)milk");
alert(testRE[1]);
But what if I have:
var firstvariable = "cow";
var secondvariable = "milk";
var test = "My cow always gives milk";
I've tried various things including:
var testRE = test.match("firstvariable(.*)secondvariable");
alert(testRE[1]);
and:
var testRE = testRE.match + '("' + firstvariable + "(.*)" + secondvariable +'")';
alert(testRE[1]);
Neither worked.