CKTIS / JavaScript
Unicode property escapes in regular expressions: L, M, N, Lu, and Lt
Match letters, combining marks, and numeric characters across scripts, and tell uppercase letters apart from titlecase letters.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
/\p{L}/u
/\p{M}/u
/\p{N}/u
/\p{Lu}/u
/\p{Lt}/u
/\p{General_Category=Letter}/u
/\P{L}/u
/[\p{L}\p{M}\p{N}]+/gu
/\p{L}/v
new RegExp("\\p{L}", "u")In JavaScript, \p{…} matches code points by a Unicode property. L, M, N, Lu, and Lt are short values of General_Category, not script names. \p{L}, \p{Letter}, and \p{General_Category=Letter} mean the same thing. Names and aliases are case-sensitive: /\p{lu}/u is a SyntaxError.
\p{L} (Letter) covers letters across scripts, including letters with no case distinction. \p{Lu} (Uppercase_Letter) and \p{Lt} (Titlecase_Letter) are disjoint subcategories of L: A is Lu, while Dž is Lt. Lt classifies an individual code point, not a word with an initial capital. Leave off i when the match must preserve case distinctions.
\p{M} (Mark) includes combining marks such as U+0301, the acute accent encoded separately after e. A regex does not normalize text: é encoded as U+00E9 has no separate M code point. Normalize before matching if you need canonically equivalent text to use a consistent representation. \p{N} (Number) includes ٢ and ½; \p{Nd} is limited to decimal digits, and \d matches only ASCII 0 through 9.
Use u or v, never both. Without either flag, \p and \P do not test Unicode properties. Without i, \P{L} matches one code point outside L, while [\p{L}\p{M}\p{N}]+ matches a run of one or more code points from any of those categories, not necessarily the whole input. ECMAScript 2024 defines both modes, but runtime support varies.
Examples
{
const text = "ADž猫e\u0301٢½!";
const categories = [
["L", /\p{L}/gu],
["M", /\p{M}/gu],
["N", /\p{N}/gu],
["Lu", /\p{Lu}/gu],
["Lt", /\p{Lt}/gu],
];
for (const [category, pattern] of categories) {
const points = (text.match(pattern) ?? []).map((char) =>
"U+" + char.codePointAt(0).toString(16).toUpperCase().padStart(4, "0")
);
console.log(`${category}: ${points.join(" ")}`);
}
}Result
L: U+0041 U+01C5 U+732B U+0065 M: U+0301 N: U+0662 U+00BD Lu: U+0041 Lt: U+01C5
The `g` flag collects all matches. The `U+…` notation makes the standalone combining mark visible, and the separate `L`, `Lu`, and `Lt` lines show which categories overlap.
Related lessons
- JavaScript · Lesson 10 · Advanced · Version: 4
Sources
- ECMAScript® 2024 Language Specification — Patterns
- ECMAScript® 2022 Language Specification — Value aliases and canonical values for the Unicode property General_Category
- ECMAScript® 2024 Language Specification — String.prototype.normalize
- Unicode character class escape: \p{...}, \P{...} — JavaScript | MDN
- RegExp.prototype.unicodeSets — JavaScript | MDN