DELAPARRA
Index
Color
NL
Category: Regex flags

/u (Unicode)

Explained by Updated

What is /u (Unicode)?

/u (Unicode): Treats the pattern as a sequence of Unicode code points, not bytes. Enables \u{...} escapes and \p{...} property matches. The u flag tells the regex engine to treat the text as Unicode characters instead of bytes. In JavaScript it enables \u{1F600} escapes and property classes like \p{L} for any letter. In PHP it makes PCRE read the subject as UTF-8.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about /u (Unicode)

How is /u (Unicode) used in practice?

Without it, characters outside the basic range are split into pieces. An emoji or an accented letter can count as two characters, so . matches half of it and length checks go wrong.

What should you keep in mind with /u (Unicode)?

Use it whenever the input can contain text written by people, which is almost always. In PHP, preg_match('/^\p{L}+$/u', $name) accepts names like Zoë and Łukasz. Without u, the same pattern rejects them or corrupts them. There is one more benefit: with u, PHP rejects invalid UTF-8 input instead of matching garbage, which surfaces encoding bugs early.

At a glance

CategoryRegex flags
Explained by
Updated

More Regex flags terms

All terms starting with a number or symbol

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.