Come posso ottenere una stringa tra due stringhe usando la corrispondenza con le variabili? Il seguente codice funziona bene se uso corrispondenza con le stringhe Regular Expression to get a string between two strings in Javascript Ho anche cercato di applicare le informazioni in JavaScript - Use variable in string match:ottiene una stringa tra due stringhe con javascript
var test = "My cow always gives milk";
var testRE = test.match("cow(.*)milk");
alert(testRE[1]);
Ma cosa succede se ho:
var firstvariable = "cow";
var secondvariable = "milk";
var test = "My cow always gives milk";
Ho provato varie cose, tra cui:
var testRE = test.match("firstvariable(.*)secondvariable");
alert(testRE[1]);
e:
var testRE = testRE.match + '("' + firstvariable + "(.*)" + secondvariable +'")';
alert(testRE[1]);
Nessuno dei due ha funzionato.
Come deve gestire un caso come "La mia mucca dà il latte mi piace il latte.". La partita non deve essere avida: "la mucca dà il latte" o l'avido: "la mucca dà il latte, mi piace il latte"? – RobG
Se stai per votare, potresti almeno dare una ragione per cui. – user3080392