【问题标题】:Javascript regex to extract title and iframe用于提取标题和 iframe 的 Javascript 正则表达式
【发布时间】:2017-01-29 07:49:31
【问题描述】:

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 或备用视频)和 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>

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

【问题讨论】:

  • 也许您可以使用 DOM 解析器而不是正则表达式。正则表达式不能很好地处理嵌套的 XML。
  • 感谢@TimBiegeleisen 的回复。需要检查 Google Apps 脚本是否有 DOM 解析器。

标签: javascript regex google-apps-script


【解决方案1】:

试试这个,应该可以的:

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

【讨论】:

  • 感谢@l-lvadim 的回复。这非常接近预期。
【解决方案2】:

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

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

Here's 一个你可以看到它工作的页面:

【讨论】:

  • 感谢@Kesty 的回复。无法像其他标题一样做出假设。
【解决方案3】:

第一个答案有效,但我认为它不是很严格。此正则表达式 [\s\S]*(Dailymotion|Alternate Video)[\s\S]*(&lt;iframe[\s\S]*&lt;\/iframe&gt;) 适用于您的示例,但如果 HTML 代码错误,则正则表达式匹配(您可以对其进行测试)。

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

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

正则表达式:

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

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

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

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

与此正则匹配:

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

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

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

【讨论】:

  • 感谢@Mattasse 的回复。我可能已经调整了这个正则表达式并再次尝试。
【解决方案4】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多