簡體   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