πŸ“ Tutorials
Β· 2 min read
Last updated on

What is Regex? A Simple Explanation for Developers


Regex (short for regular expression) is a pattern language for matching text. It lets you describe what a string should look like β€” β€œstarts with a letter, followed by 3-5 digits, ends with .com” β€” and then search, validate, or replace text that matches that pattern.

Every programming language supports regex. You’ll use it for input validation, text parsing, search-and-replace, log analysis, and data extraction.

What does regex look like?

A regex pattern is a string of characters where some characters have special meaning:

/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/

That’s an email validation pattern. It looks intimidating, but it breaks down into simple pieces:

  • ^ β€” start of string
  • [a-zA-Z0-9._%+-]+ β€” one or more letters, digits, or special chars (the local part)
  • @ β€” literal @ sign
  • [a-zA-Z0-9.-]+ β€” the domain name
  • \. β€” literal dot
  • [a-zA-Z]{2,} β€” two or more letters (the TLD like .com, .org)
  • $ β€” end of string

Common patterns you’ll actually use

\d          β†’ any digit (0-9)
\w          β†’ any word character (letter, digit, underscore)
\s          β†’ any whitespace (space, tab, newline)
.           β†’ any character except newline
+           β†’ one or more of the previous
*           β†’ zero or more of the previous
?           β†’ zero or one of the previous
{3}         β†’ exactly 3 of the previous
{2,5}       β†’ between 2 and 5 of the previous
[abc]       β†’ a, b, or c
[^abc]      β†’ anything except a, b, or c
(group)     β†’ capture group
|           β†’ or

Real-world examples

// Validate a phone number (US format)
/^\d{3}-\d{3}-\d{4}$/

// Extract all URLs from text
/https?:\/\/[^\s]+/g

// Replace multiple spaces with one
text.replace(/\s+/g, ' ')

// Check if a string is a valid hex color
/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/

// Extract numbers from a string
"Price: $42.99".match(/\d+\.?\d*/g)  // ["42.99"]

Using regex in JavaScript

// Test if a pattern matches
const isEmail = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test('user@example.com'); // true

// Find matches
const matches = 'abc 123 def 456'.match(/\d+/g); // ['123', '456']

// Replace
const clean = 'Hello   World'.replace(/\s+/g, ' '); // 'Hello World'

// Named capture groups
const match = '2026-03-15'.match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/);
console.log(match.groups.year); // '2026'

When NOT to use regex

Regex is powerful but not always the right tool:

  • Parsing HTML/XML β€” use a proper parser. Regex can’t handle nested tags reliably.
  • Complex validation β€” email validation with regex is famously incomplete. Use a library.
  • Readability matters β€” if your regex is longer than a line, consider breaking the logic into code instead.
  • Performance-critical paths β€” complex regex with backtracking can be slow. The regex CPU spike postmortem is a real example of this going wrong.

Learn more