International text

2 snippets in Regular Expressions

RXRegular Expressions

u - Unicode Flag

RX · Flags & Modifiers
syntax
JS: /pattern/u    Python: (Unicode by default in Python 3)
example
JS:  /^.$/u.test('[U+D83D][U+DE00]')    // true (emoji as one char)
JS:  /^.$/.test('[U+D83D][U+DE00]')     // false (two UTF-16 surrogates)
JS:  /\p{Emoji}/u.test('Hi')    // true (enables \p{} syntax)
output
With u: emoji treated as single character
Without u: emoji seen as two surrogate code units

Note In JS, the u flag enables full Unicode matching: surrogate pairs are treated as single code points, and Unicode property escapes (\p{...}) become available. Python 3 handles Unicode by default. Always use the u flag in JS when dealing with international text or emoji.

Unicode Property Escapes \p{...}

RX · Advanced Techniques
syntax
JS: /\p{Letter}/u    Python: regex.findall(r'\p{L}+', text)
example
JS:  'cafe\u0301 2024 Tokyo'.match(/\p{Letter}+/gu)
Py (regex module):
  import regex
  regex.findall(r'\p{N}+', 'Price: 42 or \u0664\u0662')
output
JS: ["cafe\u0301", "Tokyo"]
Py: ["42", "\u0664\u0662"]  (\p{N} matches any Unicode number)

Note Common categories: \p{L} (letter), \p{N} (number), \p{P} (punctuation), \p{S} (symbol), \p{Z} (separator), \p{Emoji}. In JS, requires the u flag. Python's built-in re does not support \p{} -- use the regex module. Essential for internationalized text processing.