patternjavascriptModerate
Character classes and Unicode property escapes
Viewed 0 times
\p{}: ES2018 (u flag); set operations: ES2024 (v flag)
character classunicode property escapeunicode regexinternational textemoji regex
Error Messages
Problem
Matching a category of characters — Unicode letters, emoji, currency symbols — requires either a long manual range or Unicode property escapes. The shorthand \w only matches ASCII.
Solution
Use Unicode property escapes with the u flag for proper Unicode categories.
/\p{L}/u.test('é'); // true — \w would miss this
/\p{Extended_Pictographic}/u // emoji
/\P{L}/u // non-letter characters
// ES2024 v flag: set intersection and subtraction
/[\p{L}--\p{ASCII}]/v // non-ASCII letters only
/\p{L}/u.test('é'); // true — \w would miss this
/\p{Extended_Pictographic}/u // emoji
/\P{L}/u // non-letter characters
// ES2024 v flag: set intersection and subtraction
/[\p{L}--\p{ASCII}]/v // non-ASCII letters only
Why
\w only matches [a-zA-Z0-9_]. Unicode property escapes handle the full Unicode character database, essential for international applications.
Gotchas
- \p{...} requires the u or v flag — throws SyntaxError without it
- \p{Emoji} matches digits (0-9) too — use \p{Extended_Pictographic} for visual emoji only
- Set operations in character classes require the v flag (ES2024)
Code Snippets
Unicode property escapes
// All Unicode letters (ES2018+)
const hasLetter = /\p{L}/u;
hasLetter.test('ñ'); // true
hasLetter.test('3'); // false
// Only non-ASCII letters (v flag, ES2024)
const nonAsciiLetter = /[\p{L}--\p{ASCII}]/v;
nonAsciiLetter.test('a'); // false
nonAsciiLetter.test('ñ'); // trueRevisions (0)
No revisions yet.