Regex•12 min read

Regular Expressions: Practical Guide for Beginners

Learn regex patterns and how to use them effectively.


Regular expressions (regex) are patterns that match strings. They're everywhere in development: validation, searching, replacing, parsing.


Regex syntax looks intimidating at first, but it's a skill worth learning.


Basic Patterns


The simplest pattern is literal text:


Pattern: cat

Matches: "cat" in "The cat sat on the mat"


But literal matching is rare. Regex becomes powerful with special characters.


Character Classes


[abc] - matches a, b, or c

[a-z] - matches any lowercase letter

[A-Z] - matches any uppercase letter

[0-9] - matches any digit

[a-zA-Z0-9] - matches any letter or digit

[^abc] - matches anything except a, b, c


Example: [aeiou] matches any vowel


Quantifiers


? - 0 or 1 times

* - 0 or more times

+ - 1 or more times

{n} - exactly n times

{n,m} - between n and m times


Examples:

  • colou?r matches "color" or "colour"
  • a+ matches "a", "aa", "aaa" etc
  • [0-9]{3} matches exactly 3 digits

Common Patterns


Email (simplified):

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


Phone (US):

[0-9]{3}-[0-9]{3}-[0-9]{4}


URL:

https?://[^\s]+


Date (YYYY-MM-DD):

[0-9]{4}-[0-9]{2}-[0-9]{2}


Meta Characters


. - any character except newline

\d - digit (0-9)

\D - non-digit

\w - word character (a-z, A-Z, 0-9, _)

\W - non-word character

\s - whitespace

\S - non-whitespace

^ - start of string

$ - end of string


Example:

  • ^The - matches "The" only at the start
  • .$ - matches the last character
  • \d{3}-\d{4} - matches "123-4567"

Anchors


^ - matches at the start

$ - matches at the end

\b - word boundary

\B - non-word boundary


Examples:

  • ^hello - "hello" at the start only
  • world$ - "world" at the end only
  • \bhello\b - "hello" as a whole word, not "helloing"

Groups


(pattern) - captures a group

(?:pattern) - non-capturing group

| - alternation (or)


Examples:

  • (cat|dog) - matches "cat" or "dog"
  • (\d{3}) groups three digits
  • colou?r can also be written as colo(u)?r

Flags


Most regex engines support flags:


  • i - case-insensitive
  • g - global (find all matches)
  • m - multiline (^ and $ match line boundaries)

Practical Examples


Validate an email:

Pattern: [a-zA-Z0-9._%plus-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}


Extract numbers:

Pattern: \\d+


Match HTML tags:

Pattern: <[^>]+>


Match URLs:

Pattern: https?://[a-zA-Z0-9.-]+\\.[a-z]{2,}


Common Mistakes


  • Forgetting to escape special characters
  • Using . when you mean a literal dot (should be \\.)
  • Forgetting ^ and $ for full match validation
  • Greedy vs non-greedy: .* is greedy, .*? is non-greedy

Greedy vs Non-Greedy


.* matches as much as possible

.*? matches as little as possible


Example text: <tag>content</tag>


Greedy: <.*> matches the entire string

Non-greedy: <.*?> matches just <tag> or </tag>


Tools


DevToolkit has a Regex Tester. Write patterns and test against sample text instantly.


Learning Tips


1. Start with simple patterns

2. Test as you build

3. Use named groups for clarity

4. Document complex regex

5. Consider simpler solutions first (sometimes split() is better than regex)


Resources


  • Regex101.com - online regex tester with explanations
  • MDN - JavaScript regex documentation
  • Perl Compatible Regular Expressions (PCRE) - most common syntax

Regex is powerful once you understand the basics. Start simple and build up.


Practice with DevToolkit

Use the tools to apply what you've learned from this guide.