DD
DevDash

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

InputResult
&✓ 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

Want API access + no ads? Pro coming soon.