JavaScript RegExp \xHH Metacharacter

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Hex byte escape

What You’ll Learn

The \xHH escape matches a character by its two-digit hexadecimal byte value (0x00–0xFF). It is a compact way to target ASCII letters, digits, and control characters—such as newline (\x0A) and tab (\x09)—without typing them literally.

01

Syntax

\x + 2 hex

02

Range

0x00–0xFF

03

Example

\x41 = A

04

Controls

\x0A LF

05

Classes

[\x30-\x39]

06

vs \u

2 vs 4 digits

Introduction

JavaScript lets you refer to characters by numeric code using escape sequences. The \xHH form uses exactly two hexadecimal digits after \x, representing a byte value from 0 to 255. That covers all of ASCII and the upper half of the Latin-1 block.

Hex escapes appear in both string literals and regex patterns. They are especially handy for control characters you cannot easily type (like line feed or null) and for keeping patterns readable when working with byte-oriented protocols or legacy data formats.

Understanding the \xHH Metacharacter

\xHH matches one character whose code unit equals the hex value. For example, \x41 is capital A (decimal 65), and \x0A is line feed (same as \n).

JavaScript
/\x41/.test("A");        // true
/\x41/.test("B");        // false
/\x0A/.test("a\nb");      // true (LF in string)

"A" === "\x41";          // true

Always use two digits. Write \x0A, not \xA, for newline. Single-digit hex after \x is invalid JavaScript syntax.

💡
Beginner Tip

Think of HH as a placeholder for two hex digits. \x41 means “match the character at byte value 0x41 (65 decimal).”

How to Use \xHH in JavaScript

Use hex escapes in detection, splitting, character classes, and normalization:

JavaScript
/\x09/.test("a\tb");              // true (tab)
"one\x0Atwo".split(/\x0A/);      // ["one", "two"]
"abc".match(/[\x61-\x7A]/g);     // lowercase a,b,c
/\x20/.test("hello world");      // true (space = 0x20)

Build ranges inside character classes with hex bounds, such as [\x30-\x39] for ASCII digits 0–9.

📝 Syntax

JavaScript
\xHH            // one byte (2 hex digits)
\x41            // letter A (0x41)
\x0A            // line feed (same as \n)
\x09            // horizontal tab (same as \t)
[\x30-\x39]     // ASCII digits 0-9
[\x41-\x5A]     // ASCII uppercase A-Z

Common patterns

  • /\x41/ — match the letter A by hex value.
  • /\x0A/g — find line feed characters.
  • /[\x30-\x39]+/ — one or more ASCII digits.
  • /\x00/ — match a null byte (see also \0).

⚡ Quick Reference

GoalPattern
Match letter A\x41
Match newline (LF)\x0A or \n
Match tab (HT)\x09 or \t
Match space\x20
ASCII digits 0–9[\x30-\x39]
Unicode (4 hex)\uXXXX

📋 \xHH vs \uXXXX vs shorthands

Three ways to match the same ASCII character or control code.

Hex byte (2 digits)
\x41

0x00 to 0xFF

Unicode (4 digits)
\u0041

U+0000 to U+FFFF

Readable shorthand
\n \t \r

Common controls

Literal
A

Same as \x41

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Examples cover basic matching, control characters, tab bytes, digit ranges, and comparison with Unicode escapes.

📚 Getting Started

Match a letter by its hex byte value.

Example 1 — Match the Letter A with \x41

Return true when the string contains 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

0x41 equals decimal 65, the ASCII code for capital A. The regex engine converts \x41 to that character before matching.

📈 Practical Patterns

Control characters, delimiters, and hex ranges.

Example 2 — Match a Newline with \x0A

Detect line feed using the explicit hex form instead of \n.

JavaScript
const text = "line1\nline2";

console.log(/\x0A/.test(text));       // true
console.log(text.split(/\x0A/));     // ["line1", "line2"]
console.log(/\n/.test(text));          // true (equivalent)
Try It Yourself

How It Works

Line feed is byte 0x0A (10 decimal). \x0A and \n match the same character; choose whichever reads clearer in your context.

Example 3 — Match a Tab with \x09

Split columns using the horizontal tab byte value.

JavaScript
const row = "name\tage";

console.log(/\x09/.test(row));    // true
console.log(row.split(/\x09/));  // ["name", "age"]
console.log(/\t/.test(row));      // true (equivalent)
Try It Yourself

How It Works

Tab is byte 0x09. Useful when parsing binary-safe exports or documentation that lists control codes in hex notation.

Example 4 — ASCII Digit Range [\x30-\x39]

Match runs of digits using hex bounds for 0 through 9.

JavaScript
const re = /[\x30-\x39]+/g;
const text = "Room 25B has code 99";

console.log(text.match(re));  // ["25", "99"]
// \x30 = "0", \x39 = "9" — same as [0-9]
Try It Yourself

How It Works

Hex ranges in character classes work like literal ranges. [\x30-\x39] is equivalent to [0-9] or \d for ASCII digits.

Example 5 — Compare \x41 and \u0041

Verify that hex and Unicode escapes match the same ASCII character.

JavaScript
const ch = "A";

console.log(/\x41/.test(ch));    // true
console.log(/\u0041/.test(ch)); // true
console.log(/^A$/.test(ch));     // true
Try It Yourself

How It Works

For code points U+0000–U+00FF, \xHH and \u00HH refer to the same character. Use \uXXXX when you need values above 0xFF.

🚀 Common Use Cases

  • Control characters — match LF, CR, tab, or null bytes by hex value.
  • Binary protocols — parse fixed byte values in network or file formats.
  • ASCII-safe patterns — write regex without non-ASCII literals in source.
  • Legacy data — handle Latin-1 bytes in old log files or dumps.
  • Hex documentation — mirror spec sheets that list codes as 0xNN.
  • Digit/letter ranges — express ASCII bounds explicitly in classes.

Important Considerations

  • Exactly two digits — pad with zero: \x0A, not \xA.
  • 255 max\xHH cannot express code points above 0xFF; use \uXXXX instead.
  • Not a modifier\x is an escape inside the pattern; the d flag is a separate modifier.
  • Prefer shorthands — for common controls, \n and \t are often clearer than hex.
  • UTF-8 awareness — multi-byte UTF-8 characters are not one \xHH unit; match Unicode explicitly when needed.

🧠 How \xHH Matches Characters

1

Parse escape

The engine reads \x and the next two hex digits.

Decode
2

Compute byte

41 hex → 65 decimal → character “A”.

Convert
3

Compare

The resolved character is checked against the next input character.

Match
4

Continue pattern

Byte matches → advance. Mismatch → fail or backtrack.

📝 Notes

  • \xHH requires exactly two hexadecimal digits (0–9, A–F).
  • The same escape works in strings: "\x41" produces "A".
  • For code points above 0xFF, use \uXXXX.
  • Previous metacharacter: \w word character.
  • Next metacharacter: \ooo octal escape.
  • Common controls: \x0A = \n, \x09 = \t, \x0D = \r.

Browser & Runtime Support

The \xHH hex byte escape is part of core JavaScript regular expression syntax.

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.

Conclusion

The \xHH metacharacter matches a character by its two-digit hexadecimal byte value (0x00–0xFF). It is ideal for ASCII letters, digits, and control characters when you want explicit numeric codes in your pattern.

Pad with leading zeros, prefer readable shorthands like \n when they exist, and switch to \uXXXX for values above 0xFF. Next, learn the \ooo octal escape metacharacter.

💡 Best Practices

✅ Do

  • Pad single-digit hex: \x0A not \xA
  • Use \xHH for control bytes in specs and protocols
  • Build ASCII ranges with hex bounds in character classes
  • Use \uXXXX when values exceed 0xFF
  • Comment the character name next to obscure hex codes

❌ Don’t

  • Omit the leading zero in two-digit hex escapes
  • Confuse \xHH (2 digits) with \uXXXX (4 digits)
  • Use hex when \n or \t reads more clearly
  • Expect \xHH to match multi-byte UTF-8 sequences
  • Mix up \x escapes with the RegExp d modifier

Key Takeaways

Knowledge Unlocked

Five things to remember about RegExp \xHH

Match any byte value from 0x00 to 0xFF with two hex digits.

5
Core concepts
📈 02

0x41

Letter A.

Example
🚫 03

\x0A

Newline.

Control
🔒 04

[\x30-\x39]

Digits.

Range
🌐 05

\uXXXX

Above 0xFF.

Extended

❓ Frequently Asked Questions

\x followed by exactly two hexadecimal digits matches the character whose byte value equals that number (0x00 to 0xFF). For example, \x41 matches A (decimal 65) and \x0A matches a line feed.
Exactly two. \x41 is valid for the letter A; \x4 (too few) is a syntax error. Always pad single-digit values with a leading zero, such as \x0A for newline.
\xHH uses two hex digits and covers values 0–255. \uXXXX uses four hex digits and covers U+0000–U+FFFF. For ASCII letters and control characters, \x41 and \u0041 match the same character.
Only if their code point fits in one byte (Latin-1 range up to \xFF). Characters above U+00FF need \uXXXX or a literal character instead.
Yes. Both match the line feed character (U+000A). \n is a readable shorthand; \x0A is the explicit hex byte form.
The \xHH escape is core JavaScript syntax since ES3. It works in every browser and Node.js version.
Did you know?

ASCII digit characters 0 through 9 sit at hex values 0x30 to 0x39. That is why [\x30-\x39] and [0-9] behave identically—they span the same byte range.

Continue to \ooo octal metacharacter

Learn another numeric escape form 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.

6 people found this page helpful