簡體   English   中英

從字符串中獲取所有 html 標簽,包括它們的內容(僅限正則表達式)

[英]Grab all html tags from string including their content (Regex Only)

我正在嘗試從字符串中無一例外地獲取所有 html 標簽。 只是為了澄清,它只需要嚴格的字符串,而不是轉換為 html 對象。 我創建了一個正則表達式,但它只抓取沒有內容的標簽。

 var text = '<div class="mura-region-local"><p>In October 2010, Lisa and Eugene Jeffers learned that their daughter Jade, then nearly 2 and a half years old, has autism. The diagnosis felt like a double whammy. The parents were soon engulfed by stress from juggling Jade's new therapy appointments and wrangling with their health insurance provider, but they now had an infant son to worry about, too. Autism runs in families. Would Bradley follow in his big sister's footsteps?</p></div><img href=""/>' var match = text.match(/<?\\w+((\\s+\\w+(\\s*=\\s*(?:".*?"|'.*?'|[\\^'">\\s]+))?)+\\s*|\\s*)?>/g); console.log(match);

您無法為所有可能的標簽找到成對的<smth>...</smth> 對於所有標簽,您也無法制作能夠識別 tagB 中的 tagA 和 tagA 中的 tagB 的正則表達式。 您必須直接編寫所有這些組合,這使得這樣的正則表達式變得不可能。

但是如果你的意思是你只想獲取<smth ....></smth><smth..../>標簽而不檢查它們的正確順序,這是可能的。

<(?:\w+(?:\s+\w+=(?:"[^"]*"|'[^']*'))*\/?|(?:\/\w+))>

是測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM