Complete Guide to Regular Expressions: Patterns, Examples, and Best Practices

Published on December 28, 2024

Regular expressions (regex) are powerful pattern-matching tools used by developers, data analysts, and anyone working with text. Whether you're validating user input, searching through logs, or extracting data, understanding regex can significantly improve your productivity and code quality.

This comprehensive guide will teach you everything you need to know about regular expressions, from basic syntax to advanced patterns. You'll learn common patterns used in real-world applications, understand regex flags, and discover best practices for writing maintainable regex code.

Quick Start: Want to test regex patterns right now? Try our free Regex Tester—it provides real-time pattern matching, match highlighting, and supports all JavaScript regex flags.

Regex Basics and Syntax

What is a Regular Expression?

A regular expression is a sequence of characters that forms a search pattern. It's used to match, find, or replace text patterns in strings. Regex is supported in most programming languages including JavaScript, Python, Java, PHP, and many text editors.

Basic Regex Characters

  • Literal Characters: Most characters match themselves. For example, hello matches the exact string "hello".
  • Dot (.): Matches any single character except newline. h.llo matches "hello", "hallo", "hxllo".
  • Asterisk (*): Matches zero or more of the preceding character. ab*c matches "ac", "abc", "abbc".
  • Plus (+): Matches one or more of the preceding character. ab+c matches "abc", "abbc" but not "ac".
  • Question Mark (?): Matches zero or one of the preceding character. colou?r matches "color" and "colour".
  • Caret (^): Matches the start of a string. ^hello matches "hello" only at the beginning.
  • Dollar ($): Matches the end of a string. world$ matches "world" only at the end.
  • Square Brackets ([]): Character class - matches any one character inside. [aeiou] matches any vowel.
  • Pipe (|): Alternation - matches either pattern. cat|dog matches "cat" or "dog".
  • Backslash (\): Escapes special characters. \\. matches a literal period.

Character Classes and Shorthand

  • \\d: Matches any digit (0-9). Equivalent to [0-9]
  • \\w: Matches any word character (letter, digit, underscore). Equivalent to [a-zA-Z0-9_]
  • \\s: Matches any whitespace character (space, tab, newline)
  • \\D: Matches any non-digit character
  • \\W: Matches any non-word character
  • \\S: Matches any non-whitespace character
  • [0-9]: Matches any digit from 0 to 9
  • [a-z]: Matches any lowercase letter
  • [A-Z]: Matches any uppercase letter
  • [^abc]: Negated character class - matches anything except a, b, or c

Frequently Asked Questions

What is a regular expression?

A regular expression (regex) is a sequence of characters that defines a search pattern. It's used to match, find, or replace text patterns in strings. Regex is supported in most programming languages and text editors for pattern matching operations.

How do I test a regular expression?

You can test regex patterns using our free online Regex Tester tool. Simply paste your pattern and test string, then see matches highlighted in real-time. The tool supports all JavaScript regex flags and provides detailed match information.

What are common regex patterns?

Common patterns include email validation (^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$), phone numbers, URLs, dates, and credit card numbers. Our Regex Tester includes a library of common patterns you can use as starting points.

What are regex flags?

Regex flags modify how the pattern matching works. Common flags include: 'g' (global - find all matches), 'i' (case-insensitive), 'm' (multiline), 's' (dotall), 'u' (unicode), and 'y' (sticky). You can combine multiple flags.

How do I escape special characters in regex?

Special characters like ., *, +, ?, ^, $, [, ], {, }, |, (, ), and \ must be escaped with a backslash (\) to match them literally. For example, to match a period, use \\. instead of .

Can I use regex in JavaScript?

Yes! JavaScript has built-in regex support through the RegExp object and regex literals (/pattern/flags). You can use methods like match(), replace(), test(), and exec() with regex patterns.

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

.* matches zero or more characters (greedy), while .+ matches one or more characters (greedy). The .* can match empty strings, while .+ requires at least one character. Use .*? or .+? for non-greedy (lazy) matching.

How do I match word boundaries?

Use \\b to match word boundaries. For example, \\bword\\b matches 'word' but not 'words' or 'password'. This is useful for finding whole words rather than substrings.

What are capture groups in regex?

Capture groups are parts of a pattern enclosed in parentheses ( ). They capture matched text for later use. For example, (\\d{3})-(\\d{3})-(\\d{4}) captures three groups from a phone number pattern.

Is regex case-sensitive?

By default, regex is case-sensitive. Use the 'i' flag to make matching case-insensitive. For example, /hello/i matches 'hello', 'Hello', 'HELLO', and 'HeLlO'.

Explore these related free tools to enhance your productivity and workflow.

Related Articles