@@ -93,15 +93,35 @@ public extension String {
9393 return " tag.fill "
9494 }
9595 }
96-
96+
97+ /// A regex that finds an opening HTML tag and captures its name.
98+ private static let htmlTagRegex : NSRegularExpression ? = {
99+ try ? NSRegularExpression (
100+ pattern: " <([A-Za-z][A-Za-z0-9]*) \\ b[^>]*> " ,
101+ options: . caseInsensitive
102+ )
103+ } ( )
104+
105+ /// Returns `true` if this string contains at least one full HTML element
106+ /// (an opening `<tag…>` **and** a matching `</tag>`).
107+ /// Email‑style `<…@…>` or other angle‑bracketed text won’t count.
97108 var isHTMLString : Bool {
98- do {
99- let regex = try NSRegularExpression ( pattern: " <[a-z][ \\ s \\ S]*> " , options: . caseInsensitive)
100- let range = NSRange ( startIndex... , in: self )
101- return regex. firstMatch ( in: self , options: [ ] , range: range) != nil
102- } catch {
103- return false
104- }
109+ guard
110+ let regex = Self . htmlTagRegex,
111+ let match = regex. firstMatch (
112+ in: self ,
113+ options: [ ] ,
114+ range: NSRange ( startIndex..< endIndex, in: self )
115+ ) ,
116+ match. numberOfRanges > 1 ,
117+ let nameRange = Range ( match. range ( at: 1 ) , in: self )
118+ else {
119+ return false
120+ }
121+
122+ // Extract the tag name and look for its closing tag
123+ let tagName = String ( self [ nameRange] )
124+ return range ( of: " </ \( tagName) > " , options: . caseInsensitive) != nil
105125 }
106126
107127 func removingHtmlTags( ) -> String {
0 commit comments