Regular Expressions for Beginners: Complete Guide

📅 Published: March 2024 | 👨‍💻 By devtools.site | 📖 12 min read

Regular expressions (regex) are one of those skills that look like magic when you first see them. That jumble of slashes, brackets, and special characters seems intimidating. But once you learn the basics, you'll wonder how you ever lived without them.

In this guide, I'll teach you regex from the ground up. No prior experience needed. By the end, you'll be able to write regex patterns for emails, phone numbers, URLs, and more.

💡 Pro Tip: Throughout this guide, you'll see regex patterns. I recommend testing them as you learn. Use the interactive regex tester at the bottom of this post!

🔍 What is a Regular Expression?

A regular expression is a sequence of characters that defines a search pattern. Think of it as a super-powered "find" feature. Instead of searching for exact text like "cat", you can search for patterns like "any 3-letter word" or "any valid email address".

📝 Simple Example

Without regex: Find "cat" → matches "cat" only.

With regex: /c.t/ → matches "cat", "cut", "cot", "c t", anything with 'c', any character, then 't'.

The dot (.) means "any single character".

📖 Basic Syntax: The Building Blocks

Literal Characters

Most characters match themselves. /hello/ matches the word "hello". Simple, right?

/hello/  → matches "hello" in "hello world"

The Dot (.) - Any Character

The dot matches ANY single character except newline.

/c.t/  → matches "cat", "cut", "c t", "c9t", etc.

Escaping Special Characters

What if you want to match an actual dot? Use backslash to escape it.

/\./  → matches a literal period (.)

Special characters that need escaping: . * + ? ^ $ { } [ ] ( ) | \

📦 Character Classes

Character classes let you match ANY character from a set.

🎯 Common Character Classes

[aeiou]  → matches any vowel
[0-9]    → matches any digit
[a-z]    → matches any lowercase letter
[A-Z]    → matches any uppercase letter
[a-zA-Z] → matches any letter (case insensitive)

Shorthand Classes

Because typing [0-9] gets tedious, regex has shortcuts:

\d

Any digit [0-9] Example: /\d/ matches "7"

\w

Word character [a-zA-Z0-9_] Example: /\w/ matches "a", "B", "9", "_"

\s

Whitespace (space, tab, newline) Example: /\s/ matches " " (space)

\D

NOT a digit Example: /\D/ matches "a", "b", "!"

\W

NOT a word character Example: /\W/ matches "!", "@", "#"

\S

NOT whitespace Example: /\S/ matches "a", "b", "9"

Negated Classes

Use ^ inside brackets to match anything EXCEPT those characters.

/[^aeiou]/  → matches any consonant or non-letter
/[^0-9]/     → matches anything that's NOT a digit

🔢 Quantifiers: How Many?

Quantifiers tell regex how many times a pattern should repeat.

🎯 Quantifiers

*   → 0 or more times    (Example: /a*/ matches "", "a", "aa", "aaa")
+   → 1 or more times    (Example: /a+/ matches "a", "aa", "aaa" but NOT "")
?   → 0 or 1 time        (Example: /colou?r/ matches "color" AND "colour")
{n} → exactly n times    (Example: /\d{3}/ matches "123" but not "12" or "1234")
{n,}→ n or more times    (Example: /\d{3,}/ matches "123", "1234", "12345")
{n,m}→ between n and m   (Example: /\d{2,4}/ matches "12", "123", "1234")
💡 Important: Quantifiers are "greedy" by default - they match as much as possible. Add a ? after to make them "lazy" (match as little as possible).
/<.*>/   → Greedy: matches "<div><span></span></div>" (everything!)
/<.*?>/  → Lazy: matches "<div>", then "<span>", etc.

⚓ Anchors: Where?

Anchors don't match characters. They match POSITIONS in the string.

^

Start of string /^Hello/ matches "Hello world" but not "Say Hello"

$

End of string /world$/ matches "Hello world" but not "world peace"

\b

Word boundary /\bcat\b/ matches "cat" but not "catalog"

\B

NOT a word boundary /\Bcat\B/ matches "catalog" but not "cat"

🔗 Groups and Capturing

Parentheses () do two things: group patterns together AND capture the matched text.

🎯 Grouping Example

/(abc)+/ → matches "abc", "abcabc", "abcabcabc"
/(\d{3})-(\d{3})-(\d{4})/ → captures area code, prefix, and line number separately

Non-Capturing Groups

Use (?:...) to group WITHOUT capturing. Saves memory when you don't need the captured value.

/(?:abc)+/ → groups but doesn't capture

Backreferences

Use captured groups later in the pattern with \1, \2, etc.

/<(\w+)>.*<\/\1>/ → matches <div>...</div> with matching tags

Named Groups

Modern regex supports named groups for better readability.

/(?<area>\d{3})-(?<prefix>\d{3})-(?<line>\d{4})/

🚩 Flags: Changing Behavior

Flags come after the closing slash and change how the regex behaves.

g (global)

Find all matches, not just the first /cat/g finds both "cat" in "cat and cat"

i (case insensitive)

Ignore case /hello/i matches "Hello", "HELLO", "hello"

m (multiline)

^ and $ match line boundaries Works across multiple lines

s (dotall)

Dot matches newlines too /.*/s matches across line breaks

u (unicode)

Full Unicode support Needed for emojis and special characters

y (sticky)

Matches only at lastIndex position Advanced use cases

💼 Practical Examples

📧 Email Validation

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

Matches: user@example.com, name.last@domain.co.uk

📞 US Phone Number

/^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/

Matches: (123)456-7890, 123-456-7890, 123.456.7890, 123 456 7890

🔗 URL Validation

/^https?:\/\/(www\.)?[a-zA-Z0-9-]+\.[a-zA-Z]{2,}(\/\S*)?$/

Matches: https://example.com, http://www.example.com/page

🗓️ Date (YYYY-MM-DD)

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

Matches: 2024-03-15, 1999-12-31

🏷️ Extract All Tags from HTML

/<\/?[a-zA-Z][^>]*>/g

Matches: <div>, </div>, <img src="...">

🔐 Strong Password

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/

Requires: 8+ chars, uppercase, lowercase, number, special character

🎨 Hex Color Code

/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/

Matches: #FFF, #FFFFFF, #abc123

📋 Quick Reference Cheat Sheet

Anchors

^ Start of string
$ End of string
\b Word boundary
\B Not word boundary

Character Classes

. Any character
\d Digit
\D Not digit
\w Word char
\W Not word char
\s Whitespace
\S Not whitespace

Quantifiers

* 0 or more
+ 1 or more
? 0 or 1
{n} Exactly n
{n,} n or more
{n,m} Between n and m

Groups & Lookarounds

(...) Capture group
(?:...) Non-capturing
(?=...) Positive lookahead
(?!...) Negative lookahead
(?<=...) Positive lookbehind
(?<!...) Negative lookbehind

⚠️ Common Pitfalls

❌ Greedy Quantifiers

/<.*>/ on "<div><span></span></div>" matches the ENTIRE string! Use /<.*?>/ for lazy matching.

❌ Unescaped Special Characters

/www.example.com/ - The dot matches ANY character, so it matches "wwwXexampleXcom" too. Use /www\.example\.com/

❌ Catastrophic Backtracking

Bad pattern: /(a+)+b/ on "aaaaaaaaaa" can take FOREVER. Be careful with nested quantifiers.

❌ Not Anchoring

/\d{3}/ matches "123" but also "1234" and "9123". Use /^\d{3}$/ to match EXACTLY three digits.

🛠️ Tools to Practice Regex

Interactive Regex Testers

Practice Regex in Real-Time

Try our free interactive regex tester. Live highlighting, flag support, and match statistics.

Try Regex Tester →

Practice Resources

🎯 Final Advice: Don't try to memorize everything. Keep a cheat sheet handy. The best way to learn regex is by PRACTICING with real data. Start with simple patterns and gradually add complexity.

Ready to Master Regex?

Use our free regex tester to practice the patterns from this guide.

Open Regex Tester →