繁体   English   中英

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

[英]Javascript regex to extract title and iframe

Google 应用程序脚本获取 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>

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

试试这个,应该工作:

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

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

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

这是一个您可以看到它工作的页面:

第一个答案有效,但我认为它不是很严格。 此正则表达式[\\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+)( [az]*=\\"[\\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+)( [az]*=\\"[\\S]*|[ ]\\.{0,1}[\\S]*\\")*)\\>).*<(iframe)( [az]*=\\"[\\S]*|[ ]\\.{0,1}[\\S]*\\")+\\><\\/\\5>\\<\\/\\3\\>|(\\<\\D+\\/\\>)$

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM