Regex for HTML Tag
Regex Pattern
<\/?[a-zA-Z][a-zA-Z0-9]*(?:\s+[^>]*)?\/?>Any HTML tag (opening, closing, or self-closing)
Quick Answer
The regex pattern for html tag is `<\/?[a-zA-Z][a-zA-Z0-9]*(?:\s+[^>]*)?\/?>`. Any HTML tag (opening, closing, or self-closing). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| <div> | ✓ Matches |
| </p> | ✓ Matches |
| <img src="x.jpg" /> | ✓ Matches |
| <h1 class="title"> | ✓ Matches |
| not a tag | ✗ No match |
| <> | ✗ No match |
| < invalid> | ✗ No match |
Code Examples
javascript
const regex = /<\\/?[a-zA-Z][a-zA-Z0-9]*(?:\s+[^>]*)?\\/?>/; const isValid = regex.test(value);
python
import re
pattern = r'<\/?[a-zA-Z][a-zA-Z0-9]*(?:\s+[^>]*)?\/?>'
if re.match(pattern, value):
print("valid")ruby
pattern = /<\/?[a-zA-Z][a-zA-Z0-9]*(?:\s+[^>]*)?\/?>/ if value =~ pattern puts "valid" end
php
if (preg_match('/<\\/?[a-zA-Z][a-zA-Z0-9]*(?:\s+[^>]*)?\\/?>/', $value)) {
echo "valid";
}java
String pattern = "<\\/?[a-zA-Z][a-zA-Z0-9]*(?:\\s+[^>]*)?\\/?>"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
HTML Comment
HTML comment block (multi-line safe)
Email Address
Validates a standard email address format
Email (RFC 5322 Compliant)
RFC 5322 compliant email validation with label length limits
URL (HTTP/HTTPS)
Validates HTTP and HTTPS URLs
URL (Any Protocol)
Matches URLs with any protocol (http, https, ftp, ws, etc.)
Domain Name
Validates a domain name (no protocol)