/u (Unicode)
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.
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
| Category | Regex flags |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |