JavaScript / ECMAScript

Hexadecimal Number Literal in JS

Match hexadecimal number literals like `0xFF`, `0x1A2B`, or `0XdeadBeef`.

Try it in the JS tester →

Pattern

regexJS
\b0[xX][0-9a-fA-F]+\b   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("\\b0[xX][0-9a-fA-F]+\\b", "g");
const input = "Mask = 0xFF; magic = 0xDEADBEEF;";
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

\b is a word boundary so we don't match inside identifiers like `var0x10`. 0[xX] requires the literal prefix (case-insensitive on the x). [0-9a-fA-F]+ matches one or more hex digits in either case. Trailing \b avoids matching into adjacent letters.

Examples

Input

Mask = 0xFF; magic = 0xDEADBEEF;

Matches

  • 0xFF
  • 0xDEADBEEF

Input

Color #ff0000 vs literal 0xff0000

Matches

  • 0xff0000

Input

no hex here

No match

Same pattern, other engines

← Back to Hexadecimal Number Literal overview (all engines)