繁体   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