Skip to content

Instantly share code, notes, and snippets.

@Sebastian2908-2007
Last active March 29, 2022 19:53
Show Gist options
  • Save Sebastian2908-2007/8fd9b2777fc0ef7f52aea177f12e9613 to your computer and use it in GitHub Desktop.
Save Sebastian2908-2007/8fd9b2777fc0ef7f52aea177f12e9613 to your computer and use it in GitHub Desktop.
This gist breaks down the regex for matching an html tag.

Matching an HTML Tag – /^<([a-z]+)([^<]+)(?:>(.)</\1>|\s+/>)$/

Summary

/^<([a-z]+)([^<]+)(?:>(.)</\1>|\s+/>)$/ - the preceeding beuty matches an html tag!

Table of Contents

Regex Components

Anchors

Anchors do not match a particular character but instead match a position before after or between characters the carrot^ matches the position before the first character in the string. The $ matches right after the last character in the string. "A regex that consists solely of an anchor can only find zero-length matches. This can be useful, but can also create complications"

Quantifiers

Quantifiers specify how many instances of a character, group, or character class must be present in the input for a match to be found. one quantifier found in this expression is * which is known a s a a greedy quantifier will match the the preceeding element zero or more times.

  • is also present several times which will match the preceeding element one ore more times. ? quantifier is also located in this regex which matches zero or one time.

OR Operator

| the pipe character AKA the or operator sepaerates terms contained in each (...) group (?:>(.*)</\1> | \s+/>) this can be found in the second part of our regex as the preeceeding example shows simply put it says "find this or find this either works"

Character Classes

A character class defines a set of characters, any one of which can occur in an input string for a match to succeed. Char classes are defined in [] example in our expression is [a-z] and [^<] you can specify a list of individual chars like the preceeding or a range like what proceeds the preceeding.

Grouping and Capturing

Capturing groups are a way to treat multiple characters as a single unit. som e example in our regex include. ([a-z]+) ([^<]+) (?:>(.*)</\1>|\s+/>) They are created by placing the characters to be grouped inside a set of parentheses.

Bracket Expressions

Bracket expressions can be used to match a single character or collating element. This example from our code will match any exept the char in the brackets [^<] this example [a-z] looks for lower case letter between a-z

Back-references

/1 is our back reference here it references the text in the first capturing group([a-z]+)

Author

Sebastian Bowen an aspiring full stack web developer. Sebastian likes long walks(cliche) but very true, Sebastian loves all things outdoors, Sebastian is amazed by modern tech and understands it's implications, Sebastian codes.

Github Profile

https://github.com/Sebastian2908-2007

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment