5 Regex Patterns That Will Save You Hours of Debugging

javascript dev.to

If you write code, you've been there — staring at a regex that should work but doesn't. Meanwhile, Stack Overflow suggests nine different patterns, none of which match your actual data.

Here are five regex patterns I reach for constantly. Each one has saved me real debugging time — the kind where you'd otherwise spend 20 minutes on a character that should have been escaped.


1. Extract Key-Value Pairs from Query Strings

/([^?&=]+)=([^&]*)/g
Enter fullscreen mode Exit fullscreen mode

Parse ?page=3&sort=desc&tag=regex into {page: "3", sort: "desc", tag: "regex"} in one line. Way cleaner than split("&")split("=") loops.

2. Match Paired HTML Tags (Without Crossing Boundaries)

/<(\w+)>[^<]*<\/\\1>/g
Enter fullscreen mode Exit fullscreen mode

The backreference \\1 ensures you match <div>...</div>, not <div>...</span>. This catches unbalanced tags that regex101 won't flag because it only tests one pattern at a time.

3. Validate ISO 8601 Dates (with Leap Year Awareness)

/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/
Enter fullscreen mode Exit fullscreen mode

A quick sanity check before passing dates to new Date(). Catches 2025-13-01 (invalid month) and 2025-02-30 (impossible day) that JavaScript silently coerces into valid dates.

4. Extract All Numbers (Including Decimals and Negatives)

/-?\d+(?:\.\d+)?/g
Enter fullscreen mode Exit fullscreen mode

Grabs -3.14, 42, 0.001 from mixed text. Use this when scraping prices, measurements, or coordinates from unstructured data.

5. Strip ANSI Escape Codes from Terminal Output

/\\x1b\[[0-9;]*m/g
Enter fullscreen mode Exit fullscreen mode

Ever piped npm test output to a file and got garbage? This pattern removes all color/formatting codes, leaving clean text you can parse or grep.


The Tool That Makes This Fast

I test every pattern above in CodeToolbox Regex Tester before pasting it into production code. Here's why:

  • Instant visual feedback — highlighted matches update as you type, so you see the greedy/lazy difference immediately
  • Capture group breakdown — each group is listed separately, so you know exactly what $1, $2 will contain
  • No data leaves your browser — all processing is local JavaScript, so you can test on real log files and API responses

No signup, no ads, no server uploads. Just a fast regex playground that stays out of your way.


These five patterns cover ~80% of the regex work I do in a typical week. What's the one regex pattern you find yourself writing over and over? I'd love to add it to my toolkit 🙂

Source: dev.to

arrow_back Back to Tutorials