·7 min read

How to Use Regex: A Beginner’s Guide to Regular Expressions

Regular expressions look intimidating at first glance, but they are one of the most powerful tools in any developer’s toolkit. This guide will take you from zero to writing your own patterns in minutes.

What Is Regex and Why Should You Learn It?

A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Think of it as a mini programming language designed specifically for finding, matching, and manipulating text.

Regex is used everywhere:

  • Form validation — checking if an email address, phone number, or URL is correctly formatted before submitting.
  • Search and replace — finding all dates in a document and reformatting them, or stripping HTML tags from text.
  • Log analysis — extracting IP addresses, error codes, or timestamps from server log files.
  • Data cleaning — removing extra whitespace, standardizing phone numbers, or extracting structured data from unstructured text.
  • Code refactoring — renaming variables, updating function calls, or migrating API patterns across a large codebase.

Once you learn regex, you will find yourself reaching for it constantly. It works in virtually every programming language (JavaScript, Python, Java, Go, PHP, Ruby) and in tools like VS Code, Sublime Text, grep, sed, and awk.

Literal Characters: The Simplest Pattern

The simplest regex is just a plain string. The pattern hello matches the exact text “hello” wherever it appears. No special syntax needed.

But regex becomes powerful when you add metacharacters — special characters that have meaning beyond their literal value.

Metacharacters: The Building Blocks

These are the core special characters you need to know:

CharacterMeaningExample
.Any single character (except newline)c.t matches “cat”, “cot”, “cut”
^Start of string (or line with m flag)^Hello matches “Hello world” but not “Say Hello”
$End of string (or line)world$ matches “Hello world”
\Escape a special character\. matches a literal period
|OR (alternation)cat|dog matches “cat” or “dog”

Character Classes: Matching Categories of Characters

Character classes let you match any character from a defined set. You write them inside square brackets:

  • [abc] — matches “a”, “b”, or “c”
  • [a-z] — matches any lowercase letter from a to z
  • [A-Z] — matches any uppercase letter
  • [0-9] — matches any digit
  • [^abc] — matches any character except a, b, or c (the ^ inside brackets means “not”)

Regex also provides shorthand character classes that save typing:

  • \d — any digit (same as [0-9])
  • \w — any “word” character: letters, digits, or underscore (same as [a-zA-Z0-9_])
  • \s — any whitespace character: space, tab, newline
  • \D, \W, \S — the negated versions (non-digit, non-word, non-whitespace)

Quantifiers: How Many Times to Match

Quantifiers control how many times a character or group should repeat:

QuantifierMeaningExample
*Zero or more timesgo*d matches “gd”, “god”, “good”, “goood”
+One or more timesgo+d matches “god”, “good” but not “gd”
?Zero or one time (optional)colou?r matches “color” and “colour”
{n}Exactly n times\d{4} matches exactly 4 digits like “2026”
{n,m}Between n and m times\d{2,4} matches 2, 3, or 4 digits

By default, quantifiers are greedy — they match as much text as possible. Add a ? after the quantifier to make it lazy (match as little as possible). For example, .+? matches one or more characters, but stops at the earliest opportunity.

Groups and Captures

Parentheses create groups that let you apply quantifiers to entire sub-patterns and extract matched text:

  • (abc) — a capturing group. Matches “abc” and remembers the match so you can reference it later (e.g., in replacements).
  • (?:abc) — a non-capturing group. Groups the pattern for quantifiers or alternation but does not store the match.

Practical example: The pattern (\d{4})-(\d{2})-(\d{2}) matches a date like “2026-03-05” and captures the year, month, and day into separate groups. In a replacement, you could rearrange them: $3/$2/$1 would produce “05/03/2026”.

Flags: Modifying Regex Behavior

Flags are added after the closing delimiter of a regex to change how the engine processes the pattern:

  • g (global) — find all matches in the string, not just the first one.
  • i (case-insensitive) — treat uppercase and lowercase letters as equivalent. /hello/i matches “Hello”, “HELLO”, and “hElLo”.
  • m (multiline) — make ^ and $ match the start and end of each line, not just the entire string.

In JavaScript, you write them after the regex literal: /pattern/gi. In Python, you pass them as arguments: re.findall(pattern, text, re.IGNORECASE).

Real-World Regex Examples

Here are patterns you can use in real projects. Try them in our free Regex Tester to see them in action.

Email Address (Basic Validation)

^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$

Matches strings like [email protected] or [email protected]. Note: email validation is notoriously complex. This pattern covers most common cases but is not RFC 5322 compliant. For production use, combine regex with server-side verification.

URL

https?:\/\/[\w.-]+(?:\.[a-zA-Z]{2,})[\/\w.-]*\/?

Matches HTTP and HTTPS URLs like https://toolbox-lab.com/tools. The s? makes the “s” in “https” optional.

Phone Number (US Format)

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

Matches formats like (555) 123-4567, 555-123-4567, and 5551234567. The ? after each separator group makes parentheses, dashes, dots, and spaces optional.

HTML Tag

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

Matches opening and closing HTML tags like <div class="box"> or </p>. Useful for stripping HTML from text, though for full HTML parsing you should use a proper DOM parser.

Tips for Writing Better Regex

  • Start simple. Write a pattern that matches your specific case first, then generalize it. Do not try to handle every edge case at once.
  • Test with real data. Paste actual sample text into a regex tester and verify your pattern matches what you expect (and does not match what it should not).
  • Use non-capturing groups when you do not need captures. Writing (?:...) instead of (...) is cleaner and slightly more efficient.
  • Be specific. Using \d instead of . when you expect digits makes your pattern more readable and reduces false matches.
  • Comment complex patterns. Most regex engines support a verbose mode (the x flag) that lets you add comments and whitespace for readability.
  • Know when not to use regex. Parsing nested structures (like HTML or JSON) with regex is fragile. Use a proper parser for those tasks.

Start Practicing

The best way to learn regex is by doing. Start with a simple pattern, test it against sample text, tweak it, and build from there. Within an hour of practice, you will be comfortable with the fundamentals covered in this guide.

Ready to try your first pattern? Open our free Regex Tester — paste your text, type a pattern, and see matches highlighted in real time. No signup required.

Try It Now — Free

Use our Regex Tester right in your browser. No signup, no upload to any server.

Open Regex Tester