\u{...}: code point escapes that need the u flag
\u{...} matches the character with the given code point, from one to six hex digits. It only means that with the u flag set; without it the pattern compiles into something else.
At a glance
- Syntax
- \u{...}
- Requires
- the u flag, without exception
- Digits
- one to six hex digits, up to 10FFFF
- Added in
- ES2015
- Quantifiers
- apply to the whole code point, unlike a surrogate pair
- Ranges
- [\u{1F600}-\u{1F64F}] is valid inside a class with u
In detail
This is the escape to use for anything outside the basic multilingual plane. It takes the code point directly, so \u{1F600} is the grinning face and \u{10FFFF} is the top of the range, and it does not require you to work out a surrogate pair by hand or to trust that someone else did.
The advantage over the pair is not just readability. Because the engine treats the code point as one unit under the u flag, a quantifier attaches to the whole character. /\u{1F600}{2}/u matches two emoji, whereas the surrogate pair spelling would attach the quantifier to the trailing half and match something that cannot exist in well formed text.
The flag requirement is absolute and the failure is silent. Without u, the braces are not part of the escape: the sequence becomes a literal letter u followed by the literal text in braces, and /\u{1F600}/ ends up searching for the eight character string u{1F600}. Worse, when the braces happen to contain only digits they parse as a quantifier instead, so /\u{2}/ matches the two character string uu.
Inside a character class the escape supports ranges, which is how emoji blocks are usually addressed. [\u{1F600}-\u{1F64F}] with the u flag covers the emoticons block, and combining a few such ranges is a workable approximation of an emoji filter, with the honest caveat that emoji sequences with modifiers and joiners are more than one code point.
Examples
/\u{1F600}/u.test("\u{1F600}"); // true
/\u{1F600}/.test("\u{1F600}"); // false, no u flag
/^\u{1F600}{2}$/u.test("\u{1F600}\u{1F600}"); // true/^[\u{1F600}-\u{1F64F}]$/u.test("\u{1F60A}"); // trueOnly the emoticons block. Emoji built from modifiers or joining characters span several code points and need more than one range.
Things that catch people out
- Forgetting the u flag does not raise an error. /\u{2}/ compiles into a search for the text uu, because the braces parse as a quantifier on a literal letter u.
- A single code point range does not cover emoji made from a base character plus a modifier or a zero width joiner, so a filter built this way misses the sequences people actually type.
- The escape is ES2015 and depends on the u flag, so it cannot be shared with any engine that does not have both.
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 →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.
\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.
\p{...}: Unicode property escapes, and the flag they need
Property escapes match by Unicode category or script instead of by code point range. Without the u flag they compile into something else entirely.
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