Pergunta

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>
Foi útil?

Solução

<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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top