Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
提取javascript中由两个不同短语分隔的大字符串中的字符串_Javascript_Jquery_Regex - Fatal编程技术网

提取javascript中由两个不同短语分隔的大字符串中的字符串

提取javascript中由两个不同短语分隔的大字符串中的字符串,javascript,jquery,regex,Javascript,Jquery,Regex,我在网上搜索了一种方法来实现这一点,到目前为止,我只遇到了一些不符合我要寻找的标准的例子 我想提取出现在“接收自:”和“发送的信息”之间的字符串 通常,字符串将显示为: 2014-01-23 13:26:13-吉姆·超人收到的邮件: 吉姆。superman@superheroes.com 拜托,我们需要一个新的超级英雄 经理的超级经理测试bla bla[wawa] 传递的信息 听起来像是一个简单的子字符串: var str = ...; // Where ever it came from

我在网上搜索了一种方法来实现这一点,到目前为止,我只遇到了一些不符合我要寻找的标准的例子

我想提取出现在“接收自:”和“发送的信息”之间的字符串

通常,字符串将显示为:

2014-01-23 13:26:13-吉姆·超人收到的邮件: 吉姆。superman@superheroes.com

拜托,我们需要一个新的超级英雄

经理的超级经理测试bla bla[wawa]

传递的信息


听起来像是一个简单的子字符串:

 var str = ...; // Where ever it came from

 str = str.substring(
         str.indexOf("received from:") + "received from:".length, 
         str.lastIndexOf("The information transmitted"));

 console.log(str); // Check the results

注意如何将长度添加到“接收自:”。因此,文本“received from:”不会包含在输出中。

您可以这样做:

var text = "...";
var fromText = "received from:";
var endText = "The information transmitted";
var extractedText = text.substring(text.indexOf(fromText) + fromText.length,
                    text.indexOf(endText));
你也可以这样做:

var extractedText = /\breceived from:(.*?)\bThe information transmitted\b/.exec(text)[1];
正则表达式怎么样

str = "2014-01-23 13:26:13 - Jim Superman received from: JIM.superman@superheroes.com..."  
/(?: received from: (.*?) The information transmitted)/.exec(str)[1]

如何尝试将您遇到的示例改编为您正在尝试的操作?如何使用
.split(start).pop().split(end)[0]
@elclanrs post作为答案?如果有人在这样的消息中发送此问题,会发生什么情况@埃尔克兰斯解决方案会在中间分裂消息。考虑到已经有其他的答案,我将删除它甚至不值得。我感谢你的答案,这就是我一直在寻找的。我对JavaScript不太熟悉,这很有效。如果同一个文本重复多次会怎么样?有没有办法阻止它被添加到str变量中?这是一个稍微不同的问题。如果有许多消息需要解析,那么在使用我给出的解析答案之前,首先需要了解如何拆分它们。您需要发布一个示例,以便找到公共分隔符。(可能是另一个问题。)