[英]Match all HTML tags except for <li> with regex
I want to check some string containing HTML to see if it will contain any <li>
elements, and filter out every element except for the <li>
elements.我想检查一些包含 HTML 的字符串,看看它是否包含任何
<li>
元素,并过滤掉除<li>
元素之外的所有元素。
For example,例如,
<li>foo</li><p><em>bar</em><em style="color: rgb(161, 0, 0);">fiddle</em></p><p><em class="ql-font-couriernew" style="color: rgb(61, 20, 102);">dog</em></p>
Would become会成为
<li>foo</li>barfiddledog
I can figure out how to capture just <li>
elements, or all HTML elements, but I can't figure out how to capture only elements that are not <li>
.我可以弄清楚如何仅捕获
<li>
元素或所有 HTML 元素,但我无法弄清楚如何仅捕获不是<li>
的元素。
I am using (<\/?[^(li)]>)+
, but this seems to only filter out the <p>
and </p>
tags.我正在使用
(<\/?[^(li)]>)+
,但这似乎只过滤掉了<p>
和</p>
标记。
Something like <\/?(?:(?.li).)+>
should do the trick.像
<\/?(?:(?.li).)+>
这样的东西应该可以解决问题。 I've taken notes from https://stackoverflow.com/a/977294/9484862 to edit your existing regex code, which was already nearly working.我已经从https://stackoverflow.com/a/977294/9484862中记下笔记来编辑您现有的正则表达式代码,该代码已经几乎可以工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.