Table of contents
Please support this book: buy it (PDF, EPUB, MOBI) or donate
(Ad, please don’t block.)

RegExp Unicode property escapes

This chapter explains the proposal “RegExp Unicode Property Escapes” by Mathias Bynens.


JavaScript lets you match characters by mentioning the “names” of sets of characters. For example, \s stands for “whitespace”:

> /^\s+$/u.test('\t \n\r')

The proposal lets you additionally match characters by mentioning their Unicode character properties (what those are is explained next) inside the curly braces of \p{}. Two examples:

> /^\p{White_Space}+$/u.test('\t \n\r')
> /^\p{Script=Greek}+$/u.test('μετά')

As you can see, one of the benefits of property escapes is is that they make regular expressions more self-descriptive. Additional benefits will become clear later.

Before we delve into how property escapes work, let’s examine what Unicode character properties are.

Unicode character properties

In the Unicode standard, each character has properties – metadata describing it. Properties play an important role in defining the nature of a character. Quoting the Unicode Standard, Sect. 3.3, D3:

The semantics of a character are determined by its identity, normative properties, and behavior.

Examples of properties

These are a few examples of properties:

Types of properties

The following types of properties exist:

Matching properties and property values

Properties and property values are matched as follows:

Unicode property escapes for regular expressions

Unicode property escapes look like this:

  1. Match all characters whose property prop has the value value:

  2. Match all characters that do not have a property prop whose value is value:

  3. Match all characters whose binary property bin_prop is True:

  4. Match all characters whose binary property bin_prop is False:


Forms (3) and (4) can also be used as an abbreviation for General_Category. For example: \p{Lowercase_Letter} is an abbreviation for \p{General_Category=Lowercase_Letter}

Important: In order to use property escapes, regular expressions must have the flag /u. Prior to /u, \p is the same as p.


Things to note:


Matching whitespace:

> /^\p{White_Space}+$/u.test('\t \n\r')

Matching letters:

> /^\p{Letter}+$/u.test('πüé')

Matching Greek letters:

> /^\p{Script=Greek}+$/u.test('μετά')

Matching Latin letters:

> /^\p{Script=Latin}+$/u.test('Grüße')
> /^\p{Script=Latin}+$/u.test('façon')
> /^\p{Script=Latin}+$/u.test('mañana')

Matching lone surrogate characters:

> /^\p{Surrogate}+$/u.test('\u{D83D}')
> /^\p{Surrogate}+$/u.test('\u{DE00}')

Note that Unicode code points in astral planes (such as emojis) are composed of two JavaScript characters (a leading surrogate and a trailing surrogate). Therefore, you’d expect the previous regular expression to match the emoji 😀, which is all surrogates:

> '😀'.length
> '😀'.charCodeAt(0).toString(16)
> '😀'.charCodeAt(1).toString(16)

However, with the /u flag, property escapes match code points, not JavaScript characters:

> /^\p{Surrogate}+$/u.test('😀')

In other words, 😀 is considered to be a single character:

> /^.$/u.test('😀')

Trying it out

V8 5.8+ implement this proposal, it is switched on via --harmony_regexp_property:

Further reading


The Unicode standard: