Regular expressions and text guides

Unicode regex: why word boundaries miss non-ASCII text

Use Unicode property escapes for letters and understand the limits of JavaScript word classes and boundaries.

Nav Tools ·

Word is an engine concept

JavaScript’s traditional \w focuses on ASCII letters, digits and underscore, with limited additional case-folding behavior in particular flag combinations. It is not all Unicode letters.

A boundary based on word and nonword transitions may not divide Chinese or other scripts as expected. Define your character policy instead of assuming natural-language segmentation.

Use Unicode properties

The Unicode-aware \p{L} property matches letters across scripts. The u flag enables the corresponding Unicode pattern syntax in this example.

Add only the categories your rule needs. A letter-only pattern excludes digits, punctuation and combining marks that might be required for actual names.

Pattern: \p{L}+
Flags: gu
Input: Ada 中文 123
Matches: Ada, 中文

Code points are not graphemes

A displayed character can contain several code points. Combining accents and emoji sequences make quantifiers over code points different from a visible character limit.

Use a grapheme segmentation API when the requirement concerns what a person sees as a character. A Unicode-aware regex does not automatically provide that segmentation.

Check normalization and confusables

Canonically equivalent text can have different underlying sequences. Normalization can help comparison but does not eliminate every visually confusing character.

Test the intended scripts, accents and mixed-script cases. Keep identifier security rules separate from a general regex that merely matches letters.

References

← Regular expressions and text guides