Regex for HTML Entity
Matches HTML entities: named (&), decimal ({), and hexadecimal (😀) forms.
Pattern
/&(#[0-9]+|#x[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);/g Live Tester
Enter a string to test
Examples
The g flag makes the pattern apply special matching behavior and match globally.
✓ &
✓ {
✓ 😀
✗ &;
✗ & amp;
✗ &#xyz;
Pattern Breakdown
& — literal ampersand
#[0-9]+ — decimal numeric entity
|#x[0-9a-fA-F]+ — hexadecimal numeric entity
|[a-zA-Z][a-zA-Z0-9]* — named entity
; — closing semicolon
Code Snippets
Javascript
const regex = /&(#[0-9]+|#x[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);/g;
"& and {".match(regex); // ["&", "{"] Python
import re
pattern = r"&(#[0-9]+|#x[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);"
re.findall(pattern, "& and {") # ['amp', '#123'] Php
$pattern = '/&(#[0-9]+|#x[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);/';
preg_match_all($pattern, "& and {", $matches); Related Patterns
Email Address /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ URL /^https?:\/\/(www\.)?[a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([a-zA-Z0-9()@:%_+.~#?&/=-]*)$/ Phone Number (International) /^\+?[1-9]\d{1,14}$/ Phone Number (US) /^\(?[2-9]\d{2}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/ Phone Number (EU) /^\+?[1-9][0-9]{0,3}[\s.-]?\(?[0-9]{1,5}\)?[\s.-]?[0-9]{1,5}[\s.-]?[0-9]{1,5}$/ IPv4 Address /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/
Frequently Asked Questions
What is the regex for HTML Entity?▾
The regex pattern for HTML Entity is /&(#[0-9]+|#x[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);/g. Matches HTML entities: named (&), decimal ({), and hexadecimal (😀) forms.
How do I use the HTML Entity regex in JavaScript?▾
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the HTML Entity pattern match?▾
It matches strings like &; it rejects strings like &;. See the Examples section above for the full list of matching and non-matching cases.