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.

Frequently asked questions

How does Regular Expressions handle readable regex?
Regular Expressions covers this with 2 copy-ready snippets on this page. The "Named Capturing Groups" snippet in Regular Expressions uses `JS: (?<name>pattern) Python: (?P<name>pattern)`.
Which pattern does the Regular Expressions example use?
The "Named Capturing Groups" snippet uses `JS: (?<name>pattern) Python: (?P<name>pattern)`, from the Groups & Alternation section of the Regular Expressions cheat sheet.
What other Regular Expressions snippets are shown for "readable regex"?
Besides "Named Capturing Groups", this page also shows "x - Verbose / Free-Spacing (Python)".
Is there anything to watch out for?
Yes. For "Named Capturing Groups": 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.