Skip to content

Instantly share code, notes, and snippets.

View amklenk's full-sized avatar

Amanda Klenk amklenk

View GitHub Profile
@amklenk
amklenk / RegexHTMLTagTutorial.md
Last active August 7, 2022 19:50
A tutorial for matching an email address in text using Regular Expressions.

Regular Expressions Tutorial: Match an HTML Tag

Regular expressions are a tool to search for patterns in code (or text). Also called RegExs, regular expressions look for text that matches the search criteria beyond an exact match that ctrl-f would return. The .* next to the case sensitivity and match-whole-word options in VS Code accesses the RegEx option. This .* is a wildcard, made up of . for any and * for the quantifier of one or more characters. Click .* to begin!

Summary

The regular expression below helps match an HTML tag in code or text. Take a look at the code below.

/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/

The components between the backslashes describe the search criteria. Start with the components that immediately follow the first backslash, as regular expressions are read from left to right, describing the beginning to end of what the search is looking for. Look for anchors to designate the begin