\xhh: matching a character by two hex digits
\xhh matches the character with the given two digit hexadecimal code, which covers U+0000 through U+00FF. Anything above that range needs \uhhhh instead.
At a glance
- Syntax
- \xhh
- Digits
- exactly two, and they are case insensitive
- Range
- U+0000 to U+00FF
- Above U+00FF
- use \uhhhh or \u{...} with the u flag
- Common values
- \x09 tab, \x0a line feed, \xa0 no break space, \x00 null
- The classic typo
- \x2028 is \x20 followed by the literal text 28
In detail
The escape exists to make an invisible character visible in the source. A no break space pasted in from a document looks exactly like an ordinary space in every editor, and a pattern that says \xa0 says clearly which one it means. The same applies to the control characters that turn up in exported data.
Exactly two digits is the whole rule, and it is where the mistakes come from. \x2028 is not the Unicode line separator; it is \x20, a space, followed by the two literal characters 2 and 8. The pattern compiles, it matches a space followed by the text 28, and it will never match the character the author was aiming at.
For anything above U+00FF the correct escape is \uhhhh with four digits, or \u{...} with the u flag. Reaching for \x on a character that does not fit is the single most common source of the previous mistake, so the rule of thumb is that \x is for the first 256 code points and nothing else.
The no break space in particular is worth knowing by number. Content management systems, word processors and rich text editors insert U+00A0 constantly, it survives a naive trim that only knows about the space character, and it makes two values that look identical compare unequal. Replacing /\xa0/g with an ordinary space early in a pipeline removes a whole class of downstream confusion.
Examples
/\x41/.test("A"); // true, 0x41 is A
/\x2028/.test("\u2028"); // false
/\x2028/.test(" 28"); // true, that is a space then the text 28"a\u00a0b".replace(/\xa0/g, " "); // "a b"The no break space is invisible in an editor and is not removed by a trim that only knows about the ordinary space.
Things that catch people out
- \x takes exactly two digits, so writing four gives a two digit escape plus literal text. The pattern compiles and matches something else entirely.
- A no break space is not an ordinary space and not removed by a hand written [ ]+ trim, though \s does match it.
- Hex escapes are unreadable at a glance. A comment naming the character is worth more here than almost anywhere else in a pattern.
Try it: Regex Tester
Type a pattern and a test string, watch every match highlight as you go, read a plain English breakdown of each token, and check the numbered and named groups it captured.
Regex Tester
LOCAL · Browser-OnlyTest and debug regular expressions with live matching and highlights
Related Regex syntax
All regex syntax →\uhhhh: matching a character by code unit
\uhhhh takes exactly four hex digits and matches one UTF-16 code unit, so anything above U+FFFF needs a surrogate pair or the braced form.
\0: matching the null character safely
\0 matches U+0000, but only while no digit follows it. \01 is an octal escape without the u flag and a syntax error with it, so \x00 is the safer spelling.
Regex to trim whitespace, collapse runs and drop blank lines
There is no preset for this one. Four short patterns cover trimming, collapsing, per line trimming and removing blank lines, and each has a different failure.
\u{...}: code point escapes that need the u flag
The braced escape addresses any code point up to 10FFFF and makes quantifiers apply to whole characters, but only when the u flag is set.
Escaping metacharacters: which ones, and where
Twelve characters need escaping outside a character class and only four inside one. User input needs an escape function before it goes near new RegExp.
Regex for email addresses: what can and cannot be validated
No regular expression fully validates an email address. Here is what the built in preset accepts, what it rejects, and the only check that settles it.
Tools for this
- Regex Tester Free online regex tester - test and debug regular expressions with live matching and highlights
- Regex Playground Regex playground with live match highlighting, named groups, find and replace, and shareable links