Regex has a reputation for being unreadable. Patterns like this don't help:

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

But there's a secret: almost every regex you'll write in practice is built from the same small set of building blocks. Learn 10–15 metacharacters and you'll be able to handle validation, extraction, and text manipulation that would otherwise take 20 lines of string operations.

This guide starts from scratch and builds up to 10 practical patterns you can use today.

What is a Regular Expression?

A regular expression (regex) is a pattern that describes a set of strings. Most programming languages — JavaScript, Python, PHP, Go, Java, Ruby — have built-in regex support. The syntax is mostly standardized, so patterns you learn transfer across languages.

The three main things you'll do with regex:

  • Test: does this string match the pattern? (form validation)
  • Extract: pull matching substrings out of text (log parsing, scraping)
  • Replace: substitute matches with new content (text transformation)

Metacharacter Cheat Sheet

SymbolMeaningExample
.Any single character (except newline)a.c → abc, aXc
*0 or more of the precedingab*c → ac, abc, abbc
+1 or more of the precedingab+c → abc, abbc (not ac)
?0 or 1 of the precedingcolou?r → color, colour
{n,m}Between n and m repetitions\d{3,4} → 123, 1234
\dAny digit (0–9)\d+ → 42, 1000
\wWord character (letter, digit, _)\w+ → hello_1
\sWhitespace (space, tab, newline)\s+ → " "
^Start of string/line^Hello → "Hello World"
$End of string/lineworld$ → "Hello world"
[abc]Any one of: a, b, or c[aeiou] → any vowel
[^abc]Any character except a, b, c[^0-9] → non-digit
()Capture group(\d{4})-(\d{2})
|ORcat|dog → cat or dog

10 Patterns Worth Memorizing

1. Email address

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

2. URL (http or https)

/https?:\/\/[^\s]+/
// Matches: https://example.com/path?q=test

3. Date (YYYY-MM-DD)

/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/
// Matches: 2024-03-15

4. US phone number

/^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/
// Matches: (555) 867-5309, 555-867-5309, +15558675309

5. Hex color code

/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/
// Matches: #ff6600, #fff

6. IPv4 address

/^(\d{1,3}\.){3}\d{1,3}$/
// Matches: 192.168.1.1

7. Alphanumeric only

/^[a-zA-Z0-9]+$/

8. Strip HTML tags

/<[^>]*>/g

9. Trim leading/trailing whitespace

/^\s+|\s+$/g

10. Slug-safe string (lowercase letters, digits, hyphens)

/^[a-z0-9]+(?:-[a-z0-9]+)*$/
// Matches: my-blog-post-2024

Using regex in JavaScript

// Test (returns boolean)
/^[a-zA-Z0-9]+$/.test("hello123") // true

// Extract (returns array)
"Meeting on 2024-03-15".match(/\d{4}-\d{2}-\d{2}/)
// ["2024-03-15"]

// Replace
"Hello   World".replace(/\s+/g, " ")
// "Hello World"

Frequently Asked Questions

Is regex slow?
Simple patterns are extremely fast — faster than most manual string parsing. The danger is catastrophic backtracking: certain patterns like (a+)+ can take exponential time on specific inputs. This is a real vulnerability if you're applying user-supplied or regex-matched input to untrusted data. Test your patterns against realistic inputs with a regex tester, and consider adding input length limits before regex validation.
What is greedy vs. lazy matching?
By default, quantifiers like * and + are greedy — they consume as many characters as possible. Add a ? after the quantifier to make it lazy (match as few characters as possible). Classic example: against <a>text</a>, the pattern <.*> matches the entire string, while <.*?> matches just <a>. For HTML parsing, lazy matching is almost always what you want.
How do I test my regex before using it in code?
Use the Regex Tester on this site — paste your pattern and test string, and get real-time match highlighting with capture group details. For deeper analysis, regex101.com breaks down each part of your pattern with explanations. Testing before deploying saves a lot of debugging time, especially for patterns that need to handle edge cases.

Key Takeaways

  • Regex is a pattern-matching syntax built into nearly every programming language
  • A small set of metacharacters (\d, \w, +, *, ^, $) handles the majority of real-world cases
  • Copy-paste common patterns for email, URLs, dates, and phone numbers rather than reinventing them
  • Always test patterns against real data — especially edge cases and adversarial inputs

The fastest way to get comfortable with regex is hands-on practice.

  • Regex Tester — test and debug patterns with real-time match highlighting
  • Text Diff Checker — compare before/after text transformations
  • Word Counter — count characters and words in your test strings