Created
August 12, 2024 16:53
-
-
Save webstrand/afdf012c03f96359893fb1dcd9ff4814 to your computer and use it in GitHub Desktop.
A regex that parses HTML. https://canary.discord.com/channels/508357248330760243/508357707602853888/1077616886063317012
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(?(DEFINE) | |
(?<S>[\x20\x9\xD\xA]+) | |
(?<NameStartChar>[A-Za-z:_\xC0-\xD6\x{D8}-\x{F6}\x{F8}-\x{2FF}\x{370}-\x{37D}\x{37F}-\x{1FFF}\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{10000}-\x{EFFFF}]) | |
(?<NameChar>(?&NameStartChar)|[\-\.0-9\x{B7}\x{0300}-\x{036F}\x{203F}-\x{2040}]) | |
(?<Name>(?&NameStartChar)(?&NameChar)*) | |
(?<EntityRef>&(?&Name);) | |
(?<PercRef>%(?&Name);) | |
(?<Reference>(?&EntityRef)|(?&PercRef)) | |
(?<AttValue>"(?:[^<&"]|(?&Reference))*"|'(?:[^<&']|(?&Reference))*'|[^"'`=<>\x20\x9\xD\xA]+) | |
(?<Attribute>(?&Name)(?:=(?:(?&AttValue))?)?) | |
(?<STag>\<(?&Name)(?:(?&S)(?&Attribute))*(?&S)?\>) | |
(?<ETag>\<\/(?&Name)>) | |
(?<Content>[^<]*) | |
(?<TagPair>\<((?&Name))(?:(?&S)(?&Attribute))*(?&S)?\>(?&HTML)*\<\/\g{-1}>) | |
(?<TagSingle>\<(?&Name)(?:(?&S)(?&Attribute))*(?&S)?\/\>) | |
(?<HTML>(?&TagPair)|(?&TagSingle)|(?&Content)) | |
) | |
(?&HTML) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment