Unicode Escape Sequence Regex
Matches JavaScript-style Unicode escape sequences, both the fixed 4-hex-digit form \uXXXX and the code-point brace form \u{XXXXX}.
Regex Pattern
\\u(?:[0-9A-Fa-f]{4}|\{[0-9A-Fa-f]{1,6}\})Default flags: g
Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| \\u | Literal backslash followed by the letter u, starting a Unicode escape |
| [0-9A-Fa-f]{4} | Exactly 4 hexadecimal digits, the classic \uXXXX escape form limited to the Basic Multilingual Plane |
| \{[0-9A-Fa-f]{1,6}\} | Braced form \u{...} holding 1 to 6 hex digits, able to represent any Unicode code point including those beyond the BMP |
| \u(?: | Non-capturing group opening right after the shared \u prefix, holding the alternation between the fixed-width and braced escape forms |
Detailed Explanation
What it does
This pattern finds literal Unicode escape sequences embedded in text or source code, matching both the traditional fixed-width \uXXXX form (exactly 4 hex digits, used for code points up to U+FFFF) and the modern braced \u{XXXXX} form introduced in ES2015 (1 to 6 hex digits, able to address the full Unicode range up to U+10FFFF).
Why it works
The two escape forms are mutually exclusive in shape, so they're expressed as an alternation after the shared \\u prefix: a bare 4-digit run for the legacy form, or a brace-delimited run of 1 to 6 digits for the extended form. Matching against the literal text of an escape sequence (not decoding it) means this pattern operates on strings that contain the two-character sequence backslash-u followed by digits, such as source code text or serialized data, rather than on already-decoded Unicode characters.
Common use cases
- Finding and validating Unicode escape sequences in JSON or JavaScript source before or during a custom parser pass
- Converting escaped Unicode sequences in log files or config strings into their actual character representation
- Auditing user-submitted code snippets for suspicious or obfuscated Unicode escapes
- Extracting all escape sequences from a template string to pre-render or cache their decoded values
Edge cases
- The legacy form always requires exactly 4 hex digits, so \u041 (3 digits) does not match, while \u0041 does
- The braced form accepts between 1 and 6 hex digits, so both \u{41} and \u{1F600} are valid, but \u{} with zero digits is not
- Surrogate pairs in the legacy form, like \uD83D\uDE00 representing a single emoji, are matched as two separate escape sequences rather than one combined code point
- Case-insensitive hex digits are supported, so \u00FF and \u00ff both match
- A lone backslash-u not followed by valid hex digits, like \uZZZZ, correctly fails to match
Limitations
- Only recognizes JavaScript/JSON/Java-style \u escapes, not other languages' escape conventions like Python's \N{...} named escapes or \U for 8-digit sequences
- Does not decode the matched escape into its actual Unicode character; that requires a separate step such as JSON.parse or String.fromCodePoint
- Does not validate that a 6-digit braced value stays within the valid Unicode range (up to 10FFFF); e.g. \u{FFFFFF} matches syntactically but exceeds the maximum code point
- Does not reconstruct surrogate pairs into a single logical character when both halves appear as separate legacy escapes
Interactive Tester
Edit the pattern or text below — matching runs live in your browser.
Test Cases
Editable — add your own inputs to see if they pass.
| Input | Expected | Result | |
|---|---|---|---|
| Pass | |||
| Fail | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass |
Language Variants
Production-ready examples in 12 languages.
const unicodeEscapeRegex = /\\u(?:[0-9A-Fa-f]{4}|\{[0-9A-Fa-f]{1,6}\})/g;
console.log('\\u0041'.match(unicodeEscapeRegex)); // ['\\u0041']Common Mistakes
Requiring exactly 4 hex digits everywhere, which rejects valid modern \u{1F600}-style escapes for characters beyond the Basic Multilingual Plane
Fix: Add an alternation for the braced \u{1,6 hex digits} form alongside the fixed 4-digit form
Confusing this literal-text pattern with actually decoding escapes, then being surprised the matched substrings aren't real Unicode characters
Fix: Follow up a successful match with a decoding step, such as JSON.parse on a wrapped string or String.fromCodePoint on the parsed hex value
Not escaping the backslash properly in the pattern source, resulting in \u being interpreted by the regex engine as its own escape instead of a literal backslash-u sequence
Fix: Escape the backslash explicitly as \\\\u in the pattern (or use a raw string literal where the language supports it) so it matches a literal backslash followed by u
Performance Notes
- Both alternatives are bounded-length matches ({4} or {1,6}), so there is no risk of catastrophic backtracking
- The g flag allows scanning an entire document for all escape sequences in a single pass rather than one match at a time
- For very large inputs, precompiling the regex once and reusing it (rather than constructing a new RegExp per call) avoids repeated compilation overhead
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |