正则表达式:h1 后跟 h2,中间没有 p

发布于 2024-09-03 01:24:52 字数 154 浏览 1 评论 0原文

我需要一个正则表达式来确定 h1 标签后面是否跟着 h2 标签,并且中间没有任何段落元素。我尝试使用负前瞻,但它不起作用:

<h1(.+?)</h1>(\s|(?!<p))*<h2(.+?)</h2>

I need a regular expression to find out whether or not a h1 tag is followed by a h2 tag, without any paragraph elements in between. I tried to use a negative lookahead but it doesn't work:

<h1(.+?)</h1>(\s|(?!<p))*<h2(.+?)</h2>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

困倦 2024-09-10 01:24:52
<h1((?!</h1).)*</h1>((?!<p).)*<h2

应该有效。

它完全匹配一个 h1 标签,然后是任意数量的字符,直到下一个 h2 标签,但前提是在整个路径中没有找到 p 标签。方式。

由于在这种情况下不太可能出现嵌套标签,因此即使使用正则表达式,这也应该非常可靠。

您需要激活工具/语言的选项,以使点匹配换行符。在正则表达式中添加 (?s) 前缀就足以实现此目的。

<h1((?!</h1).)*</h1>((?!<p).)*<h2

should work.

It matches exactly one h1 tag, then any amount of characters up to the next h2 tag, but only if no p tag is found along the way.

Since in this scenario it is rather unlikely that nested tags would occur, this should be quite reliable, even with regex.

You'll need to activate your tool's/language's option for the dot to match newline characters. It might be enough to prefix your regex with (?s) to achieve this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文