Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 38 · Syntax and Matching

Escape Sequences and Unicode Text

Match tabs, newlines, control characters and any Unicode character with regex escape sequences. Learn \n, \t, \xHH, \uHHHH, \u{...}, the u flag, surrogate pairs and emoji pitfalls.

Some characters are hard to type or impossible to see: a tab, a newline, a non-breaking space, an emoji. A regex can name any of them with an escape sequence. This lesson covers the escapes you need for everyday text work, and the Unicode details that decide whether your pattern works on international input.

Control character escapes

Escape Matches
\n newline (line feed)
\r carriage return
\t tab
\f form feed
\v vertical tab
\0 the NUL character (not followed by a digit)
\cX control character; \cJ equals \n
Node.js Tabs and line breaks

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

\r?\n is the portable way to match "a line break" on any platform: an optional carriage return followed by a line feed.

Hex and Unicode escapes

You can write any character by its code:

  • \xHH: exactly two hex digits, for code points up to FF (\x41 is A).
  • \uHHHH: exactly four hex digits, for the Basic Multilingual Plane (é is é).
  • \u{H...}: any number of hex digits, only with the u or v flag, so you can reach every code point (\u{1F600} is a grinning face).
Node.js Characters by code

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Without the u flag, \u{1F600} does not mean what you hope: it is read as the letter u repeated 1F600 times and fails. Always use u (or v) for new code.

Strings are UTF-16: surrogate pairs

JavaScript strings store text as UTF-16 code units. Characters outside the BMP (most emoji, rare scripts, some math letters) take two code units called a surrogate pair. Without the u flag, the regex engine looks at code units; with it, at whole code points.

Node.js Code units versus code points

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The class [😀] without u is really [😀]: two separate characters, so it matches each half of the emoji. The u flag fixes it.

Make u your default flag for any pattern that may meet non-English text or emoji. It also turns many mistakes (like unnecessary escapes such as \- outside classes) into early syntax errors, which is good.

Combining marks and normalization

A visible character can be several code points: é can be one code point (U+00E9) or e plus a combining accent (U+0065 U+0301). They look identical but do not match the same pattern.

Node.js Two ways to write é

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Two habits follow from this: normalize text (normalize('NFC')) before matching when input can come from different sources, and remember that "one visible letter" may be several code points. The last line is a classic trick: decompose with NFD, then remove the combining-mark range ̀-ͯ.

Case-insensitive matching and Unicode

The i flag compares case-insensitively. With u it uses Unicode case folding, so unusual pairs work.

Node.js Case folding

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Regex case-insensitivity uses simple case folding (one character to one character), so ß and SS are not equal. For linguistic comparison use localeCompare or Intl.Collator.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which pattern matches a line break on both Windows and Unix?

    Windows uses \r\n and Unix uses \n. Making the \r optional covers both.
  2. 2. What does /^.$/.test('😀') return without the u flag?

    The emoji is two UTF-16 code units, and the dot without u matches only one of them.
  3. 3. Which escape needs the u or v flag?

    The braced form \u{...} with any number of digits is available only in Unicode mode.
  4. 4. Why might /^é$/u fail on text that looks like é?

    Visually identical text can use different code point sequences. Normalize with normalize('NFC') first.

Summary

  • Use \n, \r, \t, \xHH, \uHHHH and \u{...} to name characters; \r?\n matches any line break.
  • Strings are UTF-16: without the u flag, emoji count as two characters and classes break.
  • Add u (or v) by default for text that may contain non-ASCII characters.
  • Normalize text before matching, because one letter can be one or several code points.

Next: Match Results, Indices and Iteration