Metooth Hanks
Metooth Hanks

Reputation: 37

regex that doesn't match between HTML comments

I have a regex that I want to match a certain pattern. However, I don't want it to match that pattern if it exists between HTML comment blocks

What I have currently is:

(?<!<!--)pattern(?!-->)

However that only works when the pattern is exactly between comment blocks but not in the case of something like:

<!-- foo pattern -->

But if I do:

(?<!<!--.*)pattern(?!-->)

then this case doesn't work:

<!-- some commented out stuff --> pattern

I think if I could express (everything except -->)*? within the negative look behind it would work but I'm unsure of the proper syntax or if that's allowed.

Upvotes: 1

Views: 161

Answers (1)

Emma
Emma

Reputation: 27723

My guess is that, your original expression is just fine with maybe a bit modification, we might want to have an expression similar to:

(?<=<!--).*pattern.*(?=-->)

Demo

and if we wish to capture or not-capture anything around pattern these might be of interest:

(?<=<!--).*(pattern).*(?=-->)
(?<=<!--)(.*pattern.*)(?=-->)
(?<=<!--)(.*)(pattern)(.*)(?=-->)
(?<=<!--)(?:.*)(pattern)(?:.*)(?=-->)

Upvotes: 0

Related Questions