JS: Regex Syntax. Unicode Property Escape

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2018)

Each unicode character has many properties. For exmaple, if it is: capital letter, Latin letter, punctuation, math symbol ( ± ∑ ∫ ), emoji, Chinese character, etc.

Unicode Property escape allows you to match characters by specifying their unicode properties.

note, the regex object must have flag u on or flag v on.

\p{PropertyValue}

match a char that has property PropertyValue

// get emoji chars
console.log(
 "i ♥ 😸".match(RegExp("\\p{Emoji}", "gu")),
);
// [ "♥", "😸" ]

// get chars that default to emoji presentation. this is a smaller set.
console.log(
 "i ♥ 😸".match(RegExp("\\p{Emoji_Presentation}", "gu")),
);
// [ "😸" ]
\p{PropertyName=PropertyValue}

match a char whose PropertyName is PropertyValue

// get Latin chars
console.log(
 "abc ♥ 😸. $_$".match(RegExp("\\p{Script_Extensions=Latin}", "gu")),
);
// [ "a", "b", "c" ]
\p{BinaryPropertyName}
// get punctuation chars
console.log(
 "i ♥ 😸. $_$".match(RegExp("\\p{P}", "gu")),
);
// [ ".", "_" ]
// get currency symbols
console.log(
 "i ♥ 😸. $_$".match(/\p{Sc}/gu),
);
// [ "$", "$" ]
// get characters that's unicode letter
console.log(
 "ä α ж の ♥ ⠮ 🦋 ∑ ° ⊕ +".match(/\p{L}/gu),
);
// [ "ä", "α", "ж", "の" ]

// many programing language identifier only allow unicode letter
\P{x}

(Note, uppercase P) Negation of \p{x}.