Regex for HTML tag
/<\/?[a-z][a-z0-9-]*(?:\s[^<>]*)?\/?>/gi
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
<the character “<”\/?the character “/”, optionally[a-z]one of: “a” to “z”[a-z0-9-]*one of: “a” to “z”, “0” to “9” or “-”, zero or more times, as many as possible(?:a group, not capturing\sa whitespace character[^<>]*any character except “<” or “>”, zero or more times, as many as possible)?end of group, whole group optionally\/?the character “/”, optionally>the character “>”
Other patterns
- Email address
- URL
- IPv4 address
- UUID
- Hex color
- Date (YYYY-MM-DD)
- Time (24-hour)
- US phone number
- Phone number (E.164)
- US ZIP code
- Credit card number
- URL slug
- Username
- Strong password
- HTML tag
- Semantic version
- MAC address
- JSON Web Token
- Hashtag
- @mention
- Number
- Leading and trailing whitespace
- Repeated word
- Blank lines
- Markdown link
- Quoted string
- Domain name
- File extension
- Base64
- Trailing comma