Chuyển đến nội dung

Bài 14 · Character Classes and Quantifiers

Unicode Property Escapes

Match letters, digits, punctuation, emoji and whole scripts from any language with the regex \p{...} and \P{...} property escapes in JavaScript. Includes categories, scripts and practical examples.

[a-z] and \w only know English letters. Real text contains é, ß, Ω, я, 名, ٣ and emoji. For these, JavaScript offers Unicode property escapes: \p{...} matches a character that has a given Unicode property, and \P{...} matches one that does not. They work only with the u (or v) flag.

Letters, numbers and more

The most used property is the General Category. Its short names are:

Escape Meaning
\p{L} any letter in any language
\p{Lu} / \p{Ll} uppercase / lowercase letter
\p{N} any number (digits, fractions, Roman numerals...)
\p{Nd} decimal digit in any script
\p{P} punctuation
\p{S} symbol (currency, math, emoji...)
\p{Z} separator (spaces)
\p{M} combining mark (accents)
Node.js Letters from any language

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.

\w breaks every word at an accented letter and ignores Cyrillic, Han and Greek entirely, while \p{L} handles them all. The uppercase line shows that 山田 is made of letters but not of uppercase ones: writing systems without case have no Lu or Ll characters.

Digits and numbers in other scripts

Node.js Digits everywhere

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.

\p{Nd} is the right choice for "decimal digits of any language". \p{N} is wider and includes superscripts, fractions and numeric letters.

Scripts

To select a writing system, use \p{Script=Name} (short \p{sc=Name}). To include characters used in several scripts, use \p{Script_Extensions=Name} (short scx).

Node.js Detecting a writing system

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.

Binary properties: emoji, spaces, alphabetic

Besides categories and scripts there are yes/no properties:

  • \p{Alphabetic} (alphabetic characters), \p{White_Space}, \p{ASCII}, \p{Any}.
  • \p{Emoji}, \p{Emoji_Presentation}, \p{Extended_Pictographic} for emoji detection.
Node.js Finding emoji and ASCII

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.

\p{Emoji} also matches the digits 0-9, # and * (they can start keycap emoji). For "visible pictures" use \p{Extended_Pictographic} or \p{Emoji_Presentation}. Also remember that many emoji are sequences of code points (skin tones, flags, families), so one visible emoji can match several times.

Negation and combination

\P{L} is "not a letter". You can also place property escapes inside classes: [\p{L}\p{N}_] is a Unicode-aware \w, and [^\p{L}\s] means "not a letter and not whitespace".

Node.js A Unicode-aware \w and slugs

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.

Unknown names are errors

Property names are checked when the regex is created. A typo throws a SyntaxError at once, which is helpful.

Node.js Typos fail loudly

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, \p is the letter p. Remember the flag.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which flag does \p{L} require?

    Property escapes exist only in Unicode mode. Without the flag, \p is just the letter p.
  2. 2. What does \p{Lu} match?

    Lu is the General Category "Uppercase Letter".
  3. 3. Which pattern is a Unicode-aware version of \w?

    Combine letters, numbers and the underscore. Add \p{M} if you want combining marks included.
  4. 4. How do you select Cyrillic letters only?

    Scripts use the Script (sc) or Script_Extensions (scx) property with a name.

Summary

  • \p{...} and \P{...} match by Unicode property and need the u (or v) flag.
  • Use categories (L, Lu, Nd, P, S, M), scripts (Script=Greek) and binary properties (Extended_Pictographic, ASCII).
  • [\p{L}\p{N}_] is a Unicode-aware \w; \p{M} removes accents after NFD normalization.
  • Invalid property names raise a SyntaxError immediately.

Next: Capturing Groups

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.