Google Apps脚本regex用于提取包含字符串且不包含字符串的iframe标记

时间:2016-11-15 00:30:36

标签: regex

Google Apps脚本会提取网址响应。从响应中,仅提取iframe标记并且不需要其他标记。此外,它应该将iframe与字符串 dailymotion 匹配,以将结果存储在dailymotion视频数组中,将另一个与 dailymotion 匹配,以将结果存储在其他视频数组中。

以下是回复摘录:

<section class="blog-post-content">
<h2 style="text-align: center;"><span class="wysiwyg-font-size-medium"><b>SomeThing</b></span><br></h2><div style="text-align: center;">
</div><p style="text-align: center;"></p><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.foo.bar"><span style="background-color: rgb(255, 255, 255);">http://www.foo.bar</span></a><span style="background-color: rgb(255, 255, 255);"> or </span><a href="http://www.foo.bar"><span style="background-color: rgb(255, 255, 255);">www.foo.bar</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 such as YouTube, DailyMotion, zSHARE. 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);">SomeThing Part 1<br><br></span></strong></div>
<div style="text-align: center;"><iframe src="http://www.dailymotion.com/embed/video/videoId1?autoplay=0&amp;logo=0&amp;hideInfos=0&amp;start=0&amp;syndication=202279&amp;foreground=&amp;highlight=&amp;background=" width="600" height="360" frameborder="0"></iframe></div>
<div style="text-align: center;"><strong><span style="background-color: rgb(255, 255, 255);">SomeThing Part 2<br><br></span></strong></div>
<div style="text-align: center;"><iframe src="http://www.dailymotion.com/embed/video/videoId2?autoplay=0&amp;logo=0&amp;hideInfos=0&amp;start=0&amp;syndication=202279&amp;foreground=&amp;highlight=&amp;background=" width="600" height="360" frameborder="0"></iframe></div>
<div style="text-align: center;"><strong><span style="background-color: rgb(255, 255, 255);">SomeThing Alternate Video- Part 1<br><br></span></strong></div>
<div style="text-align: center;"><iframe src="http://hqq.tv/player/embed_player.php?vid=videoId1&amp;autoplay=no" width="720" height="450" frameborder="0"></iframe></div>
<div style="text-align: center;"><strong><span style="background-color: rgb(255, 255, 255);">SomeThing Alternate Video- Part 2<br><br></span></strong></div>
<div style="text-align: center;"><iframe src="http://hqq.tv/player/embed_player.php?vid=videoId2&amp;autoplay=no" width="720" height="450" frameborder="0"></iframe></div>
</section>

直到这里

正则表达式将iframe标记与 dailymotion 视频匹配

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

正则表达式匹配 dailymotion 以外的iframe标记,搜索字符串打开或hqq。

/<iframe(.*).*(open|hqq).*\/iframe>/g

在此方法中,排除 dailymotion 意味着包含其他内容,例如 open 或hqq。没有这三个关键字的人可能会被错过。

另一种方法是通过获取所有iframe标记来管理匹配的数组元素,然后相应地推送/弹出元素。这将使代码膨胀。

仍在学习正则表达式,前瞻性负面。

0 个答案:

没有答案