Unicode property escapes
Match by what a character is, not which one it is, with \p{L}, \p{Script=Greek} and \p{Emoji}, and combine sets with the v flag. Where \w falls short.
Unicode code points and the u flagCharacter classesShorthand classes
Unicode property escapes
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- \w is [A-Za-z0-9_] in JavaScript, even with the u flag. It misses é, Ω and 日, so a name field built on \w rejects real names.
- \p{L} matches any letter in any script, \p{Lu} an uppercase one, \p{Nd} a decimal digit. \P{...} is the negation. They need the u or v flag.
- \p{Script=Greek} and \p{Script=Han} select by writing system. \p{Emoji} is broader than it sounds: it also matches the digits 0-9, # and *.
- The v flag adds set operations inside a class: difference [A--B], intersection [A&&B], nested classes, and \p{RGI_Emoji}, which matches whole emoji sequences.
- Property escapes are not universal. Python's re rejects \p, and PCRE needs UTF mode. Check the flavour and its Unicode version.
\w is an English word
You build a signup form and validate the display name with ^\w+$. It works on every test name you invent, then your first real user is called José. Rejected. So is Ωμέγα, and so is 日本語. Here is what \w+ extracted from words I ran it on, next to the pattern this lesson builds:
café \w+ -> ["caf"] \p{L}+ -> ["café"]
naïve \w+ -> ["na","ve"] \p{L}+ -> ["naïve"]
Straße \w+ -> ["Stra","e"] \p{L}+ -> ["Straße"]
Ωμέγα \w+ -> null \p{L}+ -> ["Ωμέγα"]
日本語 \w+ -> null \p{L}+ -> ["日本語"]
\w is shorthand for [A-Za-z0-9_], and the u flag does not widen it. (One oddity I found: with both i and u, \w also matches U+017F and U+212A, the long s and the Kelvin sign, because they case-fold to ASCII letters. It still misses é.) You could write out a class of every accented letter, but there are tens of thousands of letters across scripts and Unicode adds more. Nobody should type that list.
Properties instead of lists
Unicode already knows what each code point is. Every one carries a General Category (letter, number, punctuation, symbol), a Script (Latin, Greek, Han), and yes/no properties like Emoji. A property escape asks the engine to test against those:
\p{L}is any letter.\p{Lu}uppercase,\p{Ll}lowercase,\p{N}any number,\p{Nd}a decimal digit.\P{L}is the negation: any code point that is not a letter. In my test,"a1 b!".match(/\P{L}/gu)gave['1', ' ', '!'].\p{Script=Greek}selects by writing system. On"Ωμέγα abc αβγ"it found['Ωμέγα', 'αβγ'], and the short form\p{sc=Grek}worked too.\p{Script=Han}found日本語.- Long names work:
\p{Letter}and\p{General_Category=Letter}and\p{gc=L}all matchedé.
You must use the u flag (or v). Without it, \p{L} is not an error; it quietly means the letter p followed by {L}, and my test showed it matching the text p{L} and not é. An unknown name with u throws: \p{Foo} gave “Invalid property name”.
The digit trap deserves a line. /\d/u.test("٣") was false, since \d is exactly [0-9], but /\p{Nd}/u.test("٣") was true. Whether your field should accept Arabic-Indic digits is a decision about the product, not a regex trick. The regex just lets you make either choice on purpose.
\p{Emoji} is not what it sounds like
I asked which of 1 # * 😀 a match \p{Emoji}. Result: true true true true false. I believe the reason is that digits, # and * are the base characters of keycap sequences like 1️⃣ (from memory, not checked against the Unicode data files). Whatever the reason, \p{Emoji} also eats your phone numbers. \p{Emoji_Presentation} matched only 😀, and \p{Extended_Pictographic} matched 😀 and ❤. Pick the property for your actual question.
Even the right property is per code point. A family emoji 👨👩👧 is five code points (three people joined by two zero-width joiners). /\p{Emoji}/gu returned three separate matches on it.
The v flag: sets and strings
The v flag is u plus a richer class syntax. I ran these on Node 26:
"aéZb".match(/[\p{L}--[a-z]]/gv) // ["é", "Z"]
"aαβ".match(/[\p{L}&&\p{Script=Greek}]/gv) // ["α", "β"]
"a👨👩👧b".match(/\p{RGI_Emoji}/gv) // ["👨👩👧"]
-- is difference, && is intersection, and classes nest. \p{RGI_Emoji} is a property of strings, so one match can span several code points; that is why it caught the family whole. It exists only in v mode: with u it threw “Invalid property name”. v is stricter than u as well: an unescaped ( inside a class, fine under u, threw “Invalid character in character class” under v. And you cannot combine them: new RegExp("x", "uv") throws.
Other flavours
Python 3.9’s re rejects the syntax: re.compile(r'\p{L}') raised bad escape \p. A workable stand-in is [^\W\d_]+, which gave ['café', 'Ωμέγα', '日本語', 'a', 'b'] on a test string. Perl’s \p{L} and \p{Greek} worked once I told it the input was UTF-8 with -CS. pcre2grep is a trap: echo café | pcre2grep -o '\p{L}+' printed caf plus a broken byte, because without -u PCRE reads bytes. pcre2grep -u printed café. The third-party Python regex module supports \p but it was not installed here, so I did not run it.
About the video
“S8T1 - Unicode Regular Expressions for JavaScript”, from the Unicode Consortium’s channel, is a 49 minute 50 second talk from the 2021 Internationalization & Unicode Conference, published 11 February 2022. Its description names Mathias Bynens and Mark Davis as speakers and says it covers the Unicode regex standard (UTS #18), emoji “properties of strings”, and set-operator syntax for ECMAScript. That maps directly onto \p{RGI_Emoji} and the v flag. I couldn’t read a transcript and it has no chapters, so I can’t say how long it spends on the basics like \p{L}. It is a standards-committee talk about proposals, longer and denser than the rest of this roadmap’s videos, and the v flag has shipped since it was recorded. English per its title and description. It is the closest match I found, and the least focused pick in this group.
What I did not run
The Python regex module, Java and .NET property syntax, RE2, and browsers other than V8. Unicode data changes between versions, so a property’s exact membership depends on the engine’s Unicode version. I did not compare versions.
Next
Letters in non-Latin scripts raise the next question: is é equal to e plus an accent mark, and does i fold ß? That is case folding, normalization and graphemes.
Check yourself
Why does 'café'.match(/\w+/g) return ['caf'] in JavaScript?
\w means [A-Za-z0-9_] and nothing else, so é is not a word character. Use \p{L}+ with the u flag, or [\p{L}\p{N}_]+ if you want digits and underscore too.
What does [\p{L}--[a-z]] match, and which flag does it need?
Any letter except lowercase a to z, so é and Z both match but a does not. The -- difference operator exists only in v mode.
You validate a quantity with /^\d+$/u and a user in Cairo types ٣. What happens, and what would you use to accept it?
It is rejected, because JavaScript's \d is exactly [0-9] even with u. /^\p{Nd}+$/u accepts any decimal digit. Whether you should accept it is a product decision.
Does \p{Emoji} match a family emoji as a single match?
No. The family sequence is five code points and \p{Emoji} matched three of them separately in my test. With the v flag, \p{RGI_Emoji} matches the whole sequence as one.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.