Loading Developer Playground

Loading ...

Skip to main content
Development
14 min read

Regular Expressions (Regex): Complete Guide for Beginners & Advanced (2025)

Master regex patterns, metacharacters, and advanced techniques. Learn email validation, URL matching, and text processing with real examples.

halfAccessible Team

Introduction

Regular expressions (regex) are one of the most powerful tools in a developer's arsenal. Whether you're validating email addresses, extracting data from text, or performing complex search-and-replace operations, regex provides a concise and efficient solution.

In this comprehensive guide, you'll learn regex from the ground up—starting with basic patterns and progressing to advanced techniques. By the end, you'll be able to write complex regex patterns with confidence.

Practice as you learn: Use our Regex Tester & Debugger to test every example in this guide. It provides live pattern matching, syntax highlighting, and exports to multiple languages!


What Are Regular Expressions?

Regular expressions are patterns used to match character combinations in strings. They're supported in virtually every programming language and text editor.

Common Use Cases

Validation:

  • Email addresses
  • Phone numbers
  • Credit cards
  • Passwords
  • URLs and domains

Extraction:

  • Log file parsing
  • Data scraping
  • URL parameters
  • Code comments
  • Numbers from text

Transformation:

  • Search and replace
  • Text formatting
  • String sanitization
  • Code refactoring

Test patterns: Regex Tester


Basic Regex Patterns

Literal Characters

The simplest regex matches exact characters:

javascript

Try it: Regex Tester - Test: hello against "hello world"

Character Classes

Match one character from a set:

javascript

Common Shorthand Character Classes

PatternEquivalentMatches
\\d[0-9]Any digit
\\D[^0-9]Any non-digit
\\w[a-zA-Z0-9_]Word character
\\W[^a-zA-Z0-9_]Non-word character
\\s[ \\t\\n\\r]Whitespace
\\S[^ \\t\\n\\r]Non-whitespace
.Any character (except newline)
javascript

Test these patterns: Regex Tester


Quantifiers

Quantifiers specify how many times a pattern should match.

Basic Quantifiers

QuantifierMeaningExample
*0 or morea* matches "", "a", "aa", "aaa"
+1 or morea+ matches "a", "aa", "aaa"
?0 or 1 (optional)colou?r matches "color" or "colour"
{n}Exactly na{3} matches "aaa"
{n,}n or morea{2,} matches "aa", "aaa", "aaaa"
{n,m}Between n and ma{2,4} matches "aa", "aaa", "aaaa"
javascript

Greedy vs Lazy Quantifiers

javascript

Practice: Regex Tester - Try greedy vs lazy matching


Anchors and Boundaries

Anchors match positions, not characters.

Position Anchors

AnchorMatches
^Start of string/line
$End of string/line
\\bWord boundary
\\BNot a word boundary
javascript

Test boundaries: Regex Tester


Groups and Capturing

Capturing Groups

javascript

Non-Capturing Groups

javascript

Backreferences

javascript

Test groups: Regex Tester


Lookahead and Lookbehind

Positive Lookahead (?=...)

javascript

Negative Lookahead (?!...)

javascript

Positive Lookbehind (?<=...)

javascript

Negative Lookbehind (?<!...)

javascript

Practice lookarounds: Regex Tester


Common Patterns

Email Validation

javascript

URL Matching

javascript

Phone Number Validation

javascript

Password Validation

javascript

Test all patterns: Regex Tester


Flags and Modifiers

Common Flags

FlagNameDescription
gGlobalFind all matches (not just first)
iCase-insensitiveIgnore case
mMultiline^ and $ match line boundaries
sDotall. matches newlines
uUnicodeFull Unicode support
yStickyMatch from lastIndex position
javascript

Advanced Techniques

Replace with Functions

javascript

Regex with HTML/CSS

javascript

Format code: JavaScript Formatter | HTML Formatter

Log File Parsing

javascript

Performance Tips

Best Practices

🚀 Performance: Optimize regex for speed and efficiency!
javascript

Common Pitfalls

javascript

Debug patterns: Regex Tester - Visualize matches and catch issues


Regex in Different Languages

JavaScript

javascript

Python

python

PHP

php

Export patterns: Regex Tester supports JavaScript, Python, PHP, Java, and Go!


Tools and Resources

Development Tools

Our Free Tools:

Regex Cheat Sheet

Quick Reference

Character Classes:

  • . - Any character
  • \\d - Digit [0-9]
  • \\w - Word [A-Za-z0-9_]
  • \\s - Whitespace

Quantifiers:

  • * - 0 or more
  • + - 1 or more
  • ? - 0 or 1
  • {n} - Exactly n
  • {n,m} - n to m times

Anchors:

  • ^ - Start
  • $ - End
  • \\b - Word boundary

Groups:

  • (...) - Capturing group
  • (?:...) - Non-capturing
  • (?<name>...) - Named group

Lookaround:

  • (?=...) - Positive lookahead
  • (?!...) - Negative lookahead
  • (?<=...) - Positive lookbehind
  • (?<!...) - Negative lookbehind

Conclusion

Regular expressions are an essential skill for any developer. While they can seem cryptic at first, mastering regex will make you significantly more productive when working with text processing, validation, and data extraction.

Key Takeaways:

  • Start with simple patterns and build complexity gradually
  • Use character classes and quantifiers for flexible matching
  • Leverage capturing groups for data extraction
  • Test thoroughly with various inputs
  • Optimize for performance with specific patterns

Practice regex: Use our Regex Tester to test patterns in real-time with live matching, explanations, and multi-language code export!

What patterns will you build? Whether you're validating forms, parsing logs, or processing data, you now have the knowledge to write powerful regex patterns!


Master text processing with these free tools:

All tools are 100% free, require no signup, and respect your privacy.

Further Reading


Frequently Asked Questions

What's the difference between .* and .+?

.* matches zero or more characters (including empty string), while .+ matches one or more (requires at least one character). Use .*? and .+? for lazy matching.

How do I escape special characters in regex?

Use backslash \\ before special characters: . * + ? ^ $ ( ) [ ] { } | \\. For example: \\. matches a literal period. Test escaping with our Regex Tester.

What's the difference between ^ and \b?

^ matches the start of a string (or line with m flag), while \\b matches word boundaries (between \\w and \\W). ^cat matches only at string start; \\bcat\\b matches "cat" as a whole word anywhere.

Can regex parse HTML/XML reliably?

No! HTML/XML are context-free grammars that regex cannot fully parse. Use proper parsers (DOMParser, cheerio, BeautifulSoup). Use regex only for simple extraction tasks. Format HTML with our HTML Formatter.

How do I debug slow regex patterns?

Use our Regex Tester to visualize matches. Watch for catastrophic backtracking (nested quantifiers), use non-capturing groups, add anchors, and make patterns more specific.


Happy pattern matching! 🔍

Master regex with our free Regex Tester & Debugger - test patterns, see live matches, and export to any language!

Related Articles