Skip to content

Instantly share code, notes, and snippets.

View gavin-asay's full-sized avatar

Gavin Asay gavin-asay

View GitHub Profile
@gavin-asay
gavin-asay / regex_html_tag.md
Last active April 14, 2024 16:38
Regex and You: Matching an HTML Tag

Regex and You: Matching an HTML Tag

Regular expressions, ever versatile, will help up locate HTML tags in a string today.

Summary

Pattern matching HTML strings serves at least one crucial function in web dev: sanitizing user input. Allowing user-submitted strings opens one's application to significant vulnerability. Supposing, for example, some ne'er-do-well on the internet submitted a comment that includes <script src="[path]/stealYourData.js"></script>. Regular expressions allow us to match HTML tags in a string, because HTML tags conform to a certain pattern:

  • begin and end with brackets (<>)
  • contain a string name consisting of one or more lowercase letters, like p, a, div, strong, script