AI Briefing
KO

The Prickly Regex Bro Developers Try to Avoid

·2020.03.13 10:00

Key point

To overcome the limitations of special-character-removal regex, a final Unicode-based regex excluding all characters other than Korean, English, and numbers was applied.

Details

A case was introduced in which developers struggled with the complex structure of Regular Expressions (Regex) while removing special characters to prevent backend errors when entering an orderer's name. Initially, a common approach found on Stack Overflow that listed out special characters was applied, but undefined special characters kept slipping through, causing the filtering to fail.

Basic Structure and Limitations of Regex

Regex is used by combining various expressions such as \w, \s, . with flags like i (case-insensitive), g (global match), and m (multiline). The initial solution, txt.replace(/[...]/g, ''), directly listed specific special characters. However, since the types of special characters are nearly infinite, listing every case was impractical, and as a result, special characters reappeared in orderer names even after deployment.

Unicode-Based Whitelist Strategy

To solve the problem, the approach shifted from "listing special characters" to "excluding all characters other than the allowed ones (Korean, English, numbers, spaces)." After collaboration via a Slack channel (SDD), the final regex explicitly specifying Unicode ranges was completed.

The final regex (filterRegex) includes the following Unicode ranges:

  • \uAC00-\uD7A3: Complete Korean syllables (Ga-Hit)
  • \u3131-\u314E: Korean consonant jamo
  • \u314F-\u3163: Korean vowel jamo
  • \w, \s: English letters, numbers, underscore, whitespace

This approach works stably even in euc-kr encoding environments, and by also accounting for Cheonjiin keyboard characters (\u119E, \u11A2, etc.), exceptional cases were minimized. The developer recommended that regex problems too difficult to solve alone should be shared through a team Slack channel and resolved via Mob Programming.

This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.

Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.