\uhhhh: matching a character by code unit
\uhhhh matches the UTF-16 code unit with the given four digit hexadecimal value. Characters above U+FFFF are two code units, so they need a surrogate pair or \u{...} with the u flag.
At a glance
- Syntax
- \uhhhh
- Digits
- exactly four, case insensitive
- Unit
- one UTF-16 code unit, which is not always one character
- Above U+FFFF
- spell out the surrogate pair, or use \u{...} with the u flag
- Without the u flag
- \u followed by anything else is read as a literal letter u
- In a character class
- [\u0000-\u001f] is the usual control range
In detail
Four digits, one code unit. For everything in the basic multilingual plane that is also one character, so \u00e9 matches an accented e and \u3000 matches the ideographic space. It is the escape to use whenever a pattern needs a specific character that would be ambiguous, invisible or unrepresentable in the source file.
Above U+FFFF a character is two code units, and \uhhhh addresses one of them. An emoji written as a surrogate pair, \ud83d\ude00, matches correctly because the two units appear in sequence. What does not work is quantifying it: a trailing quantifier attaches to the second half of the pair only, so /\ud83d\ude00{2}/ asks for one high surrogate followed by two low ones, which is not two emoji.
The braced form fixes that. With the u flag, \u{1F600} is a single code point as far as the engine is concerned, quantifiers apply to the whole character, and the pattern reads as what it means. Without the flag the braces are not part of the escape at all, and the sequence becomes a literal letter u followed by literal text.
Inside a character class the escape is the standard way to write a range of control or formatting characters. [\u0000-\u001f] covers the C0 controls, and adding \u007f brings in delete. Stripping that range is a common first step when cleaning text that came from an unknown source.
Examples
/\u00e9/.test("é"); // true
/^\ud83d\ude00$/.test("\u{1F600}"); // true, the surrogate pair spelled out
/^\u{1F600}$/u.test("\u{1F600}"); // true, the readable form"a\u0007b".replace(/[\u0000-\u001f]/g, ""); // "ab"Things that catch people out
- A quantifier after a surrogate pair applies to the second half only, so /\ud83d\ude00{2}/ does not mean two emoji.
- Without the u flag, \u{1F600} is not a code point escape. It is a literal u followed by literal braces, and the pattern compiles without complaint.
- \u needs exactly four digits. Three digits and a letter produce a literal u followed by text, which matches nothing you intended.
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 →\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.
The u flag: Unicode mode
What the u flag changes about how a pattern is parsed, the escape forms it unlocks, and the previously tolerated syntax it turns into a hard error.
\xhh: matching a character by two hex digits
\xhh takes exactly two hex digits and covers U+0000 to U+00FF. Writing four by mistake gives a two digit escape followed by literal text.
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.
Regex for URLs: finding links without overreaching
The built in URL preset needs a scheme and a dotted host, which rules out localhost and bare ports. It also swallows a full stop at the end of a path.
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