使用javascript在两个字符串之间获取字符串 [英] get string between two strings with javascript
本文介绍了使用javascript在两个字符串之间获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用与变量匹配的两个字符串之间获取字符串?如果我使用匹配字符串,则以下代码很有效正则表达式在Javascript中获取两个字符串之间的字符串我还尝试在 JavaScript - 在字符串匹配中使用变量:
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]);
但如果我有:
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]);
和:
var testRE = testRE.match + '("' + firstvariable + "(.*)" + secondvariable +'")';
alert(testRE[1]);
两者均无效。
推荐答案
试试这个:
test.match(new RegExp(firstvariable + "(.*)" + secondvariable));
这篇关于使用javascript在两个字符串之间获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文