Unicode regex
In JavaScript, add the u flag to make the regex Unicode-aware: \p{L} matches any letter in any script, \p{N} any number, \p{Script=Greek} one script, and characters outside the Basic Multilingual Plane such as emoji count as one character instead of two code units. Without u, \w and \b are ASCII-only and \p{L} is not recognized.
Syntax
/\p{L}+/u /\p{Script=Han}/u /\p{Emoji_Presentation}/uExamples
"Zürich, Köln".match(/\p{L}+/gu)Result: ["Zürich", "Köln"]
"Zürich, Köln".match(/\w+/g)Result: ["Z", "rich", "K", "ln"]
\w is ASCII-only, so the umlauts split the words.
["😀".length, /^.$/.test("😀"), /^.$/u.test("😀")]Result: [2, false, true]
Without u, the emoji is two code units and a single dot cannot match it.
"東京 Tokyo".match(/\p{Script=Han}+/u)[0]Result: "東京"
Common mistakes
- Using \p{L} without the u flag: JavaScript then reads it as the literal text p{L} and nothing sensible matches.
- Expecting \b to respect Unicode letters; use (?<!\p{L})word(?!\p{L}) with the u flag instead.
- Measuring length with .length, which counts UTF-16 units; use [...str].length or Intl.Segmenter for user-visible characters.
- Assuming Python's re supports \p{...}; it does not. Use the third-party regex module, or rely on \w being Unicode-aware in Python 3.
Practice Unicode regex
These drills check your answer on live data, free, no account needed.
- Unicode letters with the u flagThe whole line must match. Match a single word made only of letters from any language, such as café or Zoë. Digits, spaces and punctuation are not allowed.
- Character class: gray or greyThe whole line must match. Match both spellings, gray and grey, and nothing else.
- Word boundaries: replace a whole wordReplace every whole word cat with dog. Words that only contain cat, like catalog or bobcat, stay unchanged.
Questions
How do I match any letter including accents?
/\p{L}/u matches one letter from any script, and /[\p{L}\p{M}]+/u also includes combining accent marks.
Does regex support emoji?
With the u flag, /\p{Extended_Pictographic}/u matches most emoji characters. Sequences built from several code points (skin tones, flags) still need the v flag or a dedicated library.
What is the v flag?
A newer Unicode mode (ES2024) that adds set operations such as [\p{L}--[a-z]] and string properties like \p{RGI_Emoji}. It replaces u and is stricter about escaping.
Related
- Regex for letters onlyThe ^[a-zA-Z]+$ pattern for ASCII letters, a Unicode version with \p{L} and the u flag, allowing spaces between words, and stripping non-letters from text.
- Character classesWhat a character class matches, how ranges and negation work, what \d \w \s and their uppercase opposites mean, and what the dot does and does not match.
- Word boundary \bWhat \b matches, how to find or replace whole words only, what \B does, and why \b fails next to punctuation, in string-built regexes and with Unicode text.
- Regex flagsWhat each JavaScript regex flag does (case-insensitive, global, multiline, dotAll, unicode, sticky), how to set them, and the lastIndex trap with g and test.