this post was submitted on 21 Sep 2024
303 points (96.3% liked)

Programmer Humor

32453 readers
739 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 98 points 1 month ago (14 children)

no, this is one of the worst answers on Stack Overflow

OP had a specific question to capture opening tags. The thing OP asked about can be done with regular expressions. It is true that arbitrarily nested languages like HTML cannot generally be parsed with regular expressions, but that is not what OP asked about.

[–] [email protected] 7 points 1 month ago (4 children)

It can't be done, as an opening tag in html can contain anything in its attributes, even JavaScript (e.g. onclick handler).

[–] [email protected] -1 points 1 month ago (1 children)
[–] [email protected] 5 points 1 month ago (1 children)

You can't parse every html opening tag with regex, because a html opening tag doesn't have a set structure. How would you match, with regex, this opening tag? <mytag myattribute="<value of \"myattribute\">" >

[–] [email protected] 0 points 1 month ago* (last edited 1 month ago) (1 children)

Is this valid HTML? My understanding is that that attribute value needs to be escaped, i.e. &lt;value of \&quot;myattribute\&quot;&gt;.

[–] [email protected] 4 points 1 month ago

The quote must not be escaped when you start with a single quote. The rest doesn't. This is valid and tested: <img alt='my "<img>"'>

load more comments (2 replies)
load more comments (11 replies)