Regex for HTML Entity
Regex Pattern
&(?:#[0-9]+|#x[0-9a-fA-F]+|[a-zA-Z]+);HTML character entity (named or numeric)
Quick Answer
The regex pattern for html entity is `&(?:#[0-9]+|#x[0-9a-fA-F]+|[a-zA-Z]+);`. HTML character entity (named or numeric). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| & | ✓ Matches |
| < | ✓ Matches |
| — | ✓ Matches |
| — | ✓ Matches |
| & | ✗ No match |
| & amp; | ✗ No match |
| &#; | ✗ No match |
Code Examples
javascript
const regex = /&(?:#[0-9]+|#x[0-9a-fA-F]+|[a-zA-Z]+);/; const isValid = regex.test(value);
python
import re
pattern = r'&(?:#[0-9]+|#x[0-9a-fA-F]+|[a-zA-Z]+);'
if re.match(pattern, value):
print("valid")ruby
pattern = /&(?:#[0-9]+|#x[0-9a-fA-F]+|[a-zA-Z]+);/ if value =~ pattern puts "valid" end
php
if (preg_match('/&(?:#[0-9]+|#x[0-9a-fA-F]+|[a-zA-Z]+);/', $value)) {
echo "valid";
}java
String pattern = "&(?:#[0-9]+|#x[0-9a-fA-F]+|[a-zA-Z]+);"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
HTML Tag
Any HTML tag (opening, closing, or self-closing)
HTML Comment
HTML comment block (multi-line safe)
HTML Attribute
HTML attribute key-value pair (attr="value")
HTML Self-Closing Tag
Self-closing HTML tags like <br/>, <img/>, <input/>
Semantic HTML5 Tag
HTML5 semantic element tags
XML Namespace
XML namespace declaration (xmlns or xmlns:prefix)