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
Fundamentals
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.
Concept
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).”
Usage
How to Use \xHH in JavaScript
Use hex escapes in detection, splitting, character classes, and normalization:
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
Hands-On
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.
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
\xHHExcellent
Bottom line: Safe everywhere. Use \xHH for ASCII and Latin-1 byte values up to 0xFF.
Wrap Up
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.
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
Summary
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
🔢01
Syntax
\xHH
2 hex digits
📈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.