JavaScript / ECMAScript

HTML Entity in JS

Match HTML entities in named (`&`), numeric (`{`), or hex (`💩`) form.

Try it in the JS tester →

Pattern

regexJS
&(?:[a-zA-Z][a-zA-Z0-9]+|#\d+|#x[0-9a-fA-F]+);   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("&(?:[a-zA-Z][a-zA-Z0-9]+|#\\d+|#x[0-9a-fA-F]+);", "g");
const input = "Tom & Jerry <3";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

The leading `&` and trailing `;` bracket the entity. The middle alternation matches: a named entity ([a-zA-Z][a-zA-Z0-9]+ — letters then alphanumerics, like `amp`, `lt`, `nbsp`); a decimal entity (#\d+, like `#160`); or a hex entity (#x[0-9a-fA-F]+, like `#xA0` or `#x1F600` for emoji).

Examples

Input

Tom & Jerry <3

Matches

  • &
  • <

Input

Numeric:   Hex: 😀

Matches

  •  
  • 😀

Input

no entities here

No match

Same pattern, other engines

← Back to HTML Entity overview (all engines)