liveBook · Manning

Regular expression

As explained in JavaScript in Depth

A pattern language for matching and manipulating text by describing character and sequence constraints.

A regular expression is a compact pattern for matching, searching, or manipulating text. In JavaScript, `/pattern/` creates one. The Unicode trap is that, without the `u` flag, a pattern matches UTF-16 code units: a character represented by a surrogate pair can be split across matches. section 2.1.2

One visible symbol, two matching units

With `const str = '😀ABC'`, `/^./.exec(str)` matches only the first code unit of the emoji. `/^.{3}/.exec(str)` consumes both units of the emoji and `A`. Add `u`: `/^./u` matches the emoji as one unit of the pattern, while `/^.{3}/u` matches `😀AB`. section 2.1.2

Use `u` for patterns that may encounter characters outside basic ASCII, especially when matching emoji, writing Unicode ranges, or using property escapes such as `\p{Emoji}`. It addresses surrogate-pair matching; it does not replace normalization before validating or comparing identifiers that may have different Unicode representations. section 2.1.2 section 2.3

Listing 2.3 Surprising behavior with Unicode and Regular Expressions
// Without the 'u' flag, regex treats surrogate pairs incorrectly
const str = '😀ABC';  #A
 
// Default behavior - sees surrogate pairs as two characters
/^./.exec(str); #B
/^.{3}/.exec(str);  #C
 
 
// With 'u' flag - Unicode-aware matching
/^./u.exec(str);  #D
/^.{3}/u.exec(str);  #E
 
 
// Character classes need 'u' flag for Unicode ranges
/[😀-😎]/.test('😀');  #F
/[😀-😎]/u.test('😀');  #G
 
 
// Unicode property escapes require 'u' flag
/\p{Emoji}/u.test("😀");  #H
/\p{Script=Greek}/u.test("α");  #I

Compare anchored matches, an emoji range, and Unicode property escapes with and without the `u` flag. section 2.1.2

How JavaScript in Depth covers it

How deep it goes
Works through UTF-16 surrogate-pair matching and the `u` flag, but defers detailed regular-expression coverage.
Examples
Compares emoji matches with and without `u`, then tests an emoji range and Unicode property escapes.
What it assumes
Introduces regex patterns briefly and explains the relevant UTF-16 behavior rather than assuming regular-expression expertise.
What you can do after
Apply it via JavaScript regex literals with `u` to avoid splitting surrogate pairs in Unicode patterns.
How it connects
Connects regular expressions to UTF-16, surrogate pairs, Unicode property escapes, and Unicode normalization.

Also explained in

Compare how 32 books explain regular expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.