Flag v (unicodeSets) là phiên bản nâng cấp của u, bật chế độ Unicode mạnh hơn cho character class. Không dùng chung u và v cùng lúc.
Ba tính năng chính:
- String literals nhiều ký tự trong class: [\q{...}] để khớp một chuỗi (vd emoji ghép gồm nhiều code point) như một đơn vị.
- Phép toán tập hợp trên class: giao &&, hiệu -- — lọc trực tiếp trên bộ ký tự.
- Khớp emoji có dấu/ghép chính xác hơn (grapheme nhiều code point).
// Hiệu: chữ cái nhưng KHÔNG phải nguyên âm
const consonant = /[\p{Letter}--[aeiou]]/v;
consonant.test('b'); // true
consonant.test('a'); // false
// Giao: chữ cái VÀ thuộc bảng Hy Lạp
const greek = /[\p{Letter}&&\p{Script=Greek}]/v;
// Khớp emoji ghép như một đơn vị
const flag = /[\q{🇻🇳}]/v;Hơn /u: với u, bạn không thể viết phép trừ/giao trong class hay khớp chuỗi nhiều code point — phải dùng lookahead lằng nhằng. v làm class trở thành "tập hợp" thực sự.
Lưu ý: v nghiêm ngặt hơn về escape (một số ký tự như (, ), [ trong class phải escape). Cần engine hiện đại.
The v (unicodeSets) flag is an upgraded u, enabling stronger Unicode handling in character classes. You can't combine u and v.
Three key features:
- Multi-character string literals in classes: [\q{...}] matches a string (e.g. a multi-code-point emoji) as one unit.
- Set operations on classes: intersection &&, difference -- — filter character sets directly.
- More accurate matching of composed/multi-code-point emoji (graphemes).
// Difference: letters but NOT vowels
const consonant = /[\p{Letter}--[aeiou]]/v;
consonant.test('b'); // true
consonant.test('a'); // false
// Intersection: letter AND Greek script
const greek = /[\p{Letter}&&\p{Script=Greek}]/v;
const flag = /[\q{🇻🇳}]/v;Over /u: with u you can't write subtraction/intersection in a class or match multi-code-point strings — you'd need clunky lookaheads. v makes a class a real "set".
Note: v is stricter about escaping (some chars like (, ), [ must be escaped in classes). Needs a modern engine.