Javascript正则表达式提取标题和iframe

时间:2017-01-29 07:49:32

标签: javascript regex google-apps-script

Google apps脚本获取HTTP响应内容文本。摘录如下。

<p style="text-align: left;"><span style="background-color: rgb(242, 195, 20);"><span style="color: rgb(192, 80, 77);">Disclaimer:</span></span><span style="background-color: rgb(255, 255, 255);">Please note,</span><a href="http://www.g00gl3.com"><span style="background-color: rgb(255, 255, 255);">http://www.g00gl3.com</span></a><span style="background-color: rgb(255, 255, 255);"> or </span><a href="http://www.g00gl3.com"><span style="background-color: rgb(255, 255, 255);">www.G00gl3.com</span></a><span style="background-color: rgb(255, 255, 255);"> is only video embedding websites. All of the videos found here come from 3rd party video hosting sites. We do not host any of the videos. Please contact to appropriate video hosting site for any video removal.</span></p>
<div style="text-align: center;"><strong><span style="background-color: rgb(255, 255, 255);">Dailymotion  <br><br></span></strong></div>
<div style="text-align: center;"><iframe src="http://www.dailymotion.com/embed/video/foo1234567890bar? syndication=202279" width="640" height="360" frameborder="0"></iframe></div>
<div style="text-align: center;"><strong><span style="background-color: rgb(255, 255, 255);">Alternate Video  <br><br></span></strong></div>
<div style="text-align: center;"><iframe src="http://hqq.tv/player/embed_player.php?vid=1234567890&amp;autoplay=no" width="720" height="450" frameborder="0"></iframe></div>

从这段摘录中,需要提取标题(Dailymotion或Alternate Video)和iframe。

仅匹配iframe。

/<iframe(.*)\/iframe>/g

现在预期是

Dailymotion  <br><br></span></strong></div>
<div style="text-align: center;"><iframe src="http://www.dailymotion.com/embed/video/foo1234567890bar? syndication=202279" width="640" height="360" frameborder="0"></iframe>

Alternate Video  <br><br></span></strong></div>
<div style="text-align: center;"><iframe src="http://hqq.tv/player/embed_player.php?vid=1234567890&amp;autoplay=no" width="720" height="450" frameborder="0"></iframe>

任何人都可以帮助编写正则表达式以仅在上面获取。

4 个答案:

答案 0 :(得分:1)

试试这个,应该有效:

/255\);">([a-zA-Z]+\s+.*)<br><br>/g

答案 1 :(得分:0)

假设您只需要搜索这两个标题,这将提取您需要的所有信息:

[\s\S]*(Dailymotion|Alternate Video)[\s\S]*(<iframe[\s\S]*<\/iframe>)

Here's 您可以看到它正常工作的页面:

答案 2 :(得分:0)

第一个答案工作,但我认为这不是很严格。这个正则表达式[\s\S]*(Dailymotion|Alternate Video)[\s\S]*(<iframe[\s\S]*<\/iframe>)适用于您的例子,但如果HTML代码错误则正则表达式匹配(您可以测试它)。

我让2个正则表达式更强,不方便的是正则表达式太长了。我的正则表达式的第一部分是匹配这一行:

<div style="text-align: center;"><strong><span style="background-color: rgb(255, 255, 255);">Dailymotion <br><br></span></strong></div>

正则表达式:

^(\<((\D+)( [a-z]*=\"[\S]*|[ ]\.{0,1}[\S]*\")*)\>).*(Dailymotion|Alternate Video).*\<\/\3\>|(\<\D+\/\>)$

https://regex101.com/r/XthACq/1

捕获组验证HTML是否“有效”。例如,你不能关闭。当你的html的第一行匹配时,你可以使用第二个正则表达式验证。

<div style="text-align: center;"><iframe src="http://www.dailymotion.com/embed/video/foo1234567890bar? syndication=202279" width="640" height="360" frameborder="0"></iframe></div>

这个正则表达式匹配:

^(\<((\D+)( [a-z]*=\"[\S]*|[ ]\.{0,1}[\S]*\")*)\>).*<(iframe)( [a-z]*=\"[\S]*|[ ]\.{0,1}[\S]*\")+\><\/\5>\<\/\3\>|(\<\D+\/\>)$

https://regex101.com/r/wBBOi5/1

与第一个正则表达式一样,HTML代码是验证。现在,您可以使用捕获组提取标题,链接,所有属性。

答案 3 :(得分:0)

@ l-vadim答案是最接近的,我正在使用它。

$ strace file
file.exe - System Error
---------------------------
The program can't start because cygz.dll is missing from your computer. Try
reinstalling the program to fix this problem.