Readable regex

2 snippets in Regular Expressions

RXRegular Expressions

Named Capturing Groups

RX · Groups & Alternation
syntax
JS: (?<name>pattern)    Python: (?P<name>pattern)
example
JS:  const m = '2026-04-04'.match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/)
     m.groups.year  // "2026"
Py:  m = re.search(r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})', '2026-04-04')
     m.group('year')  # '2026'
output
JS: m.groups => { year: "2026", month: "04", day: "04" }
Py: m.group('year') => '2026'

Note Syntax differs between JS and Python. JS uses (?<name>...) while Python uses (?P<name>...). Named groups dramatically improve readability of complex patterns. Both approaches also assign a numeric index.

x - Verbose / Free-Spacing (Python)

RX · Flags & Modifiers
syntax
Python: re.VERBOSE or re.X
example
Py:  pattern = re.compile(r'''
       \d{4}    # year
       -        # separator
       \d{2}    # month
       -        # separator
       \d{2}    # day
     ''', re.VERBOSE)
     pattern.search('2026-04-04')
output
Matches "2026-04-04"

Note Allows whitespace and comments in the pattern for readability. Spaces and # comments are ignored unless escaped or inside a character class. No native JS equivalent, but you can concatenate strings or use template literals to achieve similar readability.