Javascript 使用javascript获取两个字符串之间的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27656575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:28:34  来源:igfitidea点击:

get string between two strings with javascript

javascriptregexstring

提问by user3080392

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 JavascriptI also tried to apply the info at JavaScript - Use variable in string match:

如何使用变量匹配在两个字符串之间获取字符串?如果我使用匹配字符串正则表达式来获取 Javascript 中两个字符串之间的字符串,以下代码运行良好我还尝试在JavaScript 中应用信息- 在字符串匹配中使用变量

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.

都没有工作。

回答by myTerminal

Try this:

尝试这个:

test.match(new RegExp(firstvariable + "(.*)" + secondvariable));

回答by Mouser

Use this code

使用此代码

var regExString = new RegExp("(?:"+firstvariable+")((.[\s\S]*))(?:"+secondvariable+")", "ig"); //set ig flag for global search and case insensitive

var testRE = regExString.exec("My cow always gives milk.");
if (testRE && testRE.length > 1) //RegEx has found something and has more than one entry.
{  
    alert(testRE[1]); //is the matched group if found
}

This matches only the middle part of the sentence.

这仅匹配句子的中间部分。

  1. (?:"+firstvariable+")finds but does not capture cow.
  2. (.*?)captures all characters between cowand milkand saves it in a group. ?makes it lazy so it stops at milk.
  3. (?:"+secondvariable+")finds but does not capture milk.
  1. (?:"+firstvariable+")找到但不捕获cow.
  2. (.*?)捕获所有字符之间cow,并milk和一组保存它。?让它变得懒惰,所以它停在牛奶上。
  3. (?:"+secondvariable+")找到但不捕获milk.

You can test this below:

您可以在下面进行测试:

function testString()
{
    var test = document.getElementById("testStringDiv").textContent;
    var firstvariable = document.querySelectorAll("input")[0].value; //first input;
    var secondvariable = document.querySelectorAll("input")[1].value; //second input;
    var regExString = new RegExp("(?:"+firstvariable+")((.[\s\S]*))(?:"+secondvariable+")", "ig");
    var testRE = regExString.exec(test);

    if (testRE && testRE.length > 1)
    {  
      document.getElementById("showcase").textContent = testRE[1]; //return second result.
    }
}
document.getElementById("test").addEventListener("click", testString, true);
<div id="testStringDiv">My cow always gives milk.</div>
<div id="showcase">Result will display here...</div>
<input placeholder="enter first var"/><input placeholder="enter second var"/><button id="test">Search in between...</button>