Regular Expressions 101

Community Patterns

Community Library Entry

-1

Regular Expression
PCRE (PHP <7.3)

/
^(?:(?<!\/\/|\/\*|<!--).)* #check if commented out <[a-z][a-z0-9]*?\b #find start of a tag (?:.(?![^?]>))+ #Must not find > between start and class unless ?> class=(["']) #Find class and opening quotes (?:(?!\1).)* \bmatch\b (?:(?!\1).)* #see if our tag is in between the quotes \1 #closing quotes .*(?<!\?)> #find first > without ? in front of it
/
gmUix

Description

I needed a regex to find the opening location of an html tag with a specific class. It is ugly, and it relies on many false assumtions, yet be honest, this will work in most cases. Also checks if the tag is in a line that is commented out. Again, we wont know if it is commented out if it is done on another line, yet in most cases this will work.

Submitted by Stack overflow Superspy - 10 years ago