JavaScript RegExp \xHH Hex Escape

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is \xHH?

\xHH matches one character by its two-digit hexadecimal byte value (0x00–0xFF). It covers ASCII letters, digits, and control characters such as newline (\x0A) and tab (\x09).

Remember
\xHH     → character at hex byte HH

\x41     → "A"   (65 decimal)
\x0A     → newline  (same as \n)
\x09     → tab      (same as \t)
\x20     → space

exactly 2 hex digits (pad with 0)
range: 0x00 … 0xFF
UsePrefer
Letter A by code\x41 or A
Newline / tab\n / \t (or \x0A / \x09)
Above 0xFF\uXXXX

How to Use \xHH

Write two hex digits after \x, then test or match as usual.

  1. Pick the character’s byte value in hex (0–FF). Pad with a leading zero if needed.
  2. Put \xHH in a regex literal or RegExp pattern — e.g. /\x41/.
  3. Call test, match, or split on your string.
  4. For common controls, prefer \n / \t when readability matters; use hex when a spec lists 0xNN codes.
PieceRequired?Role
\xYesStarts a hex byte escape
Two hex digitsYes0–9, A–F (case-insensitive)
Character classOptionale.g. [\x30-\x39] for ASCII digits

Pseudocode

Pseudocode
pattern contains "\x" + HH   // HH = two hex digits

byte = parse_hex(HH)         // 0 … 255
target = character(byte)

for each char in input:
    if char == target:
        match this position
    else:
        try next / fail

Cheat Sheet

Keep these hex escapes handy while writing patterns.

TopicDetail
Syntax\xHH (exactly two hex digits)
Letter A\x41
Newline (LF)\x0A or \n
Tab (HT)\x09 or \t
Space\x20
ASCII digits[\x30-\x39] (= [0-9])
vs Unicode\uXXXX for values above 0xFF
StandardES3+ (universal)

Live Preview

Pick a hex escape and see what character it matches against a sample string.

Browser demo of \xHH — same idea as the examples below.

Result
/\x41/.test("ABC")
→ true

/* 0x41 = 65 = "A" */

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Match the Letter A with \x41

Use a hex escape to test for the character at byte 0x41.

JavaScript
console.log(/\x41/.test("A"));    // true
console.log(/\x41/.test("B"));    // false
console.log(/\x41/.test("ABC"));  // true
Try It Yourself

How It Works

  1. Decode the escape. \x41 means hex 41 → decimal 65 → capital A.
  2. Test a single letter. /\x41/.test("A") finds that character → true.
  3. Reject a neighbor. B is 0x42, so the same pattern returns false.
  4. Match inside a longer string. "ABC" still contains A → true.

Example 2 — Newline \x0A and Tab \x09

Detect control characters with explicit hex values (same as \n and \t).

JavaScript
const text = "line1\nline2";
const row = "name\tage";

console.log(/\x0A/.test(text));   // true
console.log(text.split(/\x0A/));  // ["line1", "line2"]
console.log(/\x09/.test(row));    // true
console.log(row.split(/\x09/));   // ["name", "age"]
Try It Yourself

How It Works

  1. Line feed is 0x0A. \x0A matches the same character as \n.
  2. Split on LF. split(/\x0A/) breaks the string into two lines.
  3. Tab is 0x09. \x09 matches the same character as \t.
  4. Split columns. Useful when a protocol or dump lists control codes in hex.

Example 3 — Digit Range and \x vs \u

Match ASCII digits with a hex class, and confirm \x41 equals \u0041.

JavaScript
const text = "Room 25B has code 99";
console.log(text.match(/[\x30-\x39]+/g));  // ["25", "99"]

const ch = "A";
console.log(/\x41/.test(ch));    // true
console.log(/\u0041/.test(ch));  // true
Try It Yourself

How It Works

  1. Hex range in a class. \x30 is "0", \x39 is "9" — same as [0-9].
  2. Global match. /[\x30-\x39]+/g finds digit runs "25" and "99".
  3. Same ASCII character. For U+0000–U+00FF, \xHH and \u00HH match the same code.
  4. When to switch. Need values above 0xFF? Use \uXXXX.

Edge Cases & Pitfalls

Check these before relying on \xHH in real patterns.

syntax

Exactly two hex digits

Write \x0A, not \xA. A single digit after \x is a syntax error.

range

Max value is 0xFF

\xHH cannot express code points above 255. Use \uXXXX (or a literal) instead.

readability

Prefer \n / \t when clear

Hex is great for specs and protocols. For everyday newlines and tabs, shorthands are easier to read.

unicode

Not a UTF-8 byte matcher

JavaScript strings are UTF-16 code units. Multi-byte UTF-8 sequences are not one \xHH unit.

Browser & Runtime Support

The \xHH hex byte escape is part of core JavaScript regular expression syntax (ES3+).

✓ Baseline · ES3+

RegExp \xHH

Supported in every browser and Node.js version. \x plus two hex digits matches that byte value.

99% Universal syntax
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
\xHH Excellent

Bottom line: Safe everywhere. Use \xHH for ASCII and Latin-1 byte values up to 0xFF; switch to \uXXXX above that.

Performance Notes

ApproachCostBest for
/\x41/O(n) scanMatching one fixed character
/A/ literalO(n) scanReadable ASCII letters (same cost)
[\x30-\x39]O(n) scanASCII digit runs; same as [0-9]

Hex escapes are resolved when the pattern is compiled. At match time they behave like a single literal character — no extra runtime cost versus writing A or \n directly.

Key Takeaways

  • Role: match one character by two-digit hex byte (0x00–0xFF).
  • Pad zeros: always two digits — \x0A, never \xA.
  • Controls: \x0A = \n, \x09 = \t.
  • Above 255: use \uXXXX.

One line: \xHH is a compact hex byte escape — pad to two digits, stay within 0xFF, prefer \n/\t when clearer.

Frequently Asked Questions

\x followed by exactly two hexadecimal digits matches the character whose byte value equals that number (0x00–0xFF). For example, \x41 matches A (65) and \x0A matches a line feed.
Exactly two. \x41 is valid for A; \x4 alone is a syntax error. Pad single-digit values with a leading zero, such as \x0A for newline.
\xHH uses two hex digits (0–255). \uXXXX uses four hex digits (U+0000–U+FFFF). For ASCII, \x41 and \u0041 match the same character.
Yes. Both match the line feed character (U+000A). \n is the readable shorthand; \x0A is the explicit hex form.
No. Values above 0xFF need \uXXXX (or a literal / Unicode escape). \xHH only covers one byte.
\xHH is core JavaScript syntax since ES3. It works in every modern browser and Node.js version.

Next: \ooo octal escape

Learn another numeric escape used in JavaScript regular expressions.

\ooo tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful