Unicode & Property Escapes
Real-world text isn't just A–Z. Names, cities and messages contain accents, other scripts and emoji. Classic regex tools like [a-z], \w and \b were designed for ASCII, so they quietly fail on text such as Nguyễn, Zürich or 東京.
/[A-Za-z]+/g
Nguyễn Văn An lives in Zürich9 matches
#1 "Nguy" at index 0
#2 "n" at index 5
#3 "V" at index 7
#4 "n" at index 9
#5 "An" at index 11
#6 "lives" at index 14
#7 "in" at index 20
#8 "Z" at index 23
#9 "rich" at index 25
Names are chopped into pieces. Unicode property escapes solve this.
Property escapes: \p{...}
With the u or v flag, \p{Property} matches any character that has a Unicode property, and \P{Property} matches any character that doesn't.
| Escape | Matches |
|---|---|
\p{L} / \p{Letter} |
any letter, any script |
\p{Lu} / \p{Ll} |
uppercase / lowercase letter |
\p{N} / \p{Nd} |
any number / decimal digit |
\p{P} |
punctuation |
\p{M} |
combining mark (accents stored separately) |
\p{Script=Han} |
Chinese characters |
\p{Script=Latin} |
Latin-script letters |
\p{Emoji_Presentation} |
characters shown as emoji by default |
\p{RGI_Emoji} |
complete emoji sequences (v flag only) |
/\p{L}+/gu
Nguyễn Văn An lives in Zürich6 matches
#1 "Nguyễn" at index 0
#2 "Văn" at index 7
#3 "An" at index 11
#4 "lives" at index 14
#5 "in" at index 20
#6 "Zürich" at index 23
Scripts
Script= (short: sc=) selects a writing system:
/\p{Script=Han}+|\p{Script=Hiragana}+/gu
東京 means "eastern capital"; とうきょう is the reading.2 matches
#1 "東京" at index 0
#2 "とうきょう" at index 28
A Unicode-aware "word"
A word made of letters, combining marks, digits and connectors is a good stand-in for \w in international text:
/[\p{L}\p{M}\p{N}_]+/gu
Café №5 — naïve façade, 価格 200円6 matches
#1 "Café" at index 0
#2 "5" at index 6
#3 "naïve" at index 10
#4 "façade" at index 16
#5 "価格" at index 24
#6 "200円" at index 27
Emoji and the v flag
A single visible emoji can be several code points — a skin-tone modifier, a joiner, a variation selector. \p{RGI_Emoji} with the v flag matches the whole sequence:
const s = "Team: 👩🏽💻 👍 🇻🇳";
console.log(s.match(/\p{Emoji_Presentation}/gu).length);
console.log(s.match(/\p{RGI_Emoji}/gv));6
[ '👩🏽💻', '👍', '🇻🇳' ]
Normalization: one letter, two spellings
é can be stored as one code point (U+00E9) or as e + a combining accent (U+0065 U+0301). They look identical but don't match each other. Normalize text before matching:
const composed = "café";
const decomposed = "café";
console.log(composed === decomposed);
console.log(/café/u.test(decomposed));
console.log(/café/u.test(decomposed.normalize("NFC")));false
false
true
Case-insensitive Unicode
With i + u, case folding follows Unicode rules, so /đà/iu matches ĐÀ. Python 3 does this by default for str patterns:
import re
print(re.findall(r"\w+", "Nguyễn Văn An"))
print(re.findall(r"đà nẵng", "ĐÀ NẴNG, Đà Nẵng", flags=re.I))
print(re.findall(r"\w+", "Nguyễn Văn An", flags=re.ASCII))['Nguyễn', 'Văn', 'An']
['ĐÀ NẴNG', 'Đà Nẵng']
['Nguy', 'n', 'V', 'n', 'An']
Note: Python's built-in
redoes not support\p{...}. The third-partyregexpackage does, as do Java, .NET, PCRE, Go and Rust.
Test yourself
Which pattern matches a run of letters from any language in JavaScript?
/\w+//[a-z]+/i/\p{L}+/u/\p{L}+/
Why might /café/ fail to match text that displays as "café"?
- Regex can't match accents
- The text may use a decomposed
e+ combining accent - It needs the
gflag - The pattern must be lowercase
Which flag is required for \p{RGI_Emoji}?
uvsy