Regex for HTML tag

/<\/?[a-z][a-z0-9-]*(?:\s[^<>]*)?\/?>/gi

Options
Matches

Examples

Note

Finds tags, not structure. Nested or malformed HTML needs a real parser, like the browser’s DOMParser.

Matches in the example

  • "<p class=\"lead\">"
  • "<strong>"
  • "</strong>"
  • "<br/>"
  • "</p>"

Token by token

  1. < the character “<”
  2. \/? the character “/”, optionally
  3. [a-z] one of: “a” to “z”
  4. [a-z0-9-]* one of: “a” to “z”, “0” to “9” or “-”, zero or more times, as many as possible
  5. (?: a group, not capturing
  6. \s a whitespace character
  7. [^<>]* any character except “<” or “>”, zero or more times, as many as possible
  8. )? end of group, whole group optionally
  9. \/? the character “/”, optionally
  10. > the character “>”

Other patterns