Regex for Beginners: Learn Regular Expressions with 10 Practical Examples
Regular expressions look cryptic, but a dozen symbols cover 90% of uses. Learn the basics of regex with concrete examples — emails, phone numbers, dates — and test them live.
What Is a Regex?
A regular expression (regex) is a pattern that describes a set of strings. Instead of searching for an exact word, you describe its shape: "three digits, a dash, four digits", "a word that starts with a capital letter", "anything between two quotes".
Regex are everywhere: form validation, search and replace in code editors, log analysis, data cleaning, routing in web frameworks. They look cryptic at first, but a small number of symbols covers most uses.
The Basic Building Blocks
Characters and Classes
| Symbol | Matches | Example |
|---|---|---|
abc | The exact text "abc" | cat finds "cat" in "concatenate" |
. | Any character (except line break) | c.t → cat, cut, c9t |
\d | A digit (0–9) | \d\d → 42 |
\w | A letter, digit or underscore | \w+ → a word |
\s | A space, tab or line break | a\sb → "a b" |
[abc] | One of the listed characters | gr[ae]y → gray, grey |
[a-z] | A character in the range | [A-Z] → a capital letter |
[^abc] | Any character except those | [^0-9] → not a digit |
Quantifiers
| Symbol | Meaning | Example |
|---|---|---|
* | 0 or more times | ab*c → ac, abc, abbbc |
+ | 1 or more times | \d+ → 7, 42, 2026 |
? | 0 or 1 time (optional) | colou?r → color, colour |
{3} | Exactly 3 times | \d{3} → 123 |
{2,4} | Between 2 and 4 times | \d{2,4} → 12, 123, 1234 |
Anchors and Groups
| Symbol | Meaning |
|---|---|
^ | Start of the text (or of the line with the m flag) |
$ | End of the text (or of the line) |
\b | Word boundary |
( ) | Capture group: extracts a part of the match |
The vertical bar means "or": cat|dog matches "cat" or "dog".
Test Your Regex Live
The regex tester highlights matches as you type, shows capture groups and includes a cheat sheet. It uses the JavaScript engine, the same as your browser and Node.js. Your patterns run in a separate worker with a time limit, so a badly written regex can't freeze the page.
10 Practical Examples
- A whole number:
^\d+$ - A decimal number (with a dot or comma):
^\d+([.,]\d+)?$ - A simple email check:
^[^\s@]+@[^\s@]+\.[^\s@]+$ - A US ZIP code:
^\d{5}(-\d{4})?$ - A date in YYYY-MM-DD format:
^\d{4}-\d{2}-\d{2}$ - A time in 24h format:
^([01]\d|2[0-3]):[0-5]\d$ - A hex color:
^#([0-9a-fA-F]{3}){1,2}$ - A URL starting with http or https:
^https?://\S+$ - Double spaces to clean up:
\s{2,}(replace with a single space) - Words in capitals (acronyms):
\b[A-Z]{2,}\b
Don't validate everything with regex
A regex can check the shape of an email, not that the address exists. For dates, a regex accepts "2026-02-31": check the actual value in code. Regex are a first filter, not a full validation.
The Flags
Flags change how the whole pattern behaves:
- g (global): finds all matches, not just the first.
- i (case-insensitive): ignores uppercase/lowercase.
- m (multiline):
^and$match the start and end of each line. - s (dotAll): the dot also matches line breaks.
Common Beginner Mistakes
- Forgetting to escape special characters: to search for a literal dot, write
\., not.. - Forgetting the anchors:
\d{5}also matches inside "123456789". Use^\d{5}$to validate a whole field. - Greedy quantifiers:
".*"on"a" and "b"captures everything from the first to the last quote. Use the lazy version".*?". - Nested quantifiers like
(a+)+: they can take an exponential time on some texts (catastrophic backtracking).
Use Regex Without Writing Code
Many AwesomeToolkit text tools accept regex: find and replace to transform a text in bulk (with capture groups in the replacement), and text search to find and count all occurrences.
Conclusion
You don't need to memorize the whole syntax: classes (\d, \w, [...]), quantifiers (+, *, ?, {n}) and anchors (^, $) cover the vast majority of needs. Write the pattern, test it live on real examples, and adjust until every case is right.
Share this article
Related articles
How to Format and Validate JSON (and Fix the 7 Most Common Errors)
Unreadable JSON on one line? "Unexpected token" error? Learn how to beautify, validate and minify JSON, and how to fix the syntax errors developers hit most often.
JWT Explained: How JSON Web Tokens Work (with Examples)
What's inside a JWT, how the signature works, what the exp, iat and sub claims mean, and the security mistakes to avoid — with a decoded example.
How to Turn Code into a Beautiful Image (for Slides, Docs and Social Media)
Share code on LinkedIn, X or in a presentation as a sharp, highlighted image instead of a blurry screenshot. Themes, backgrounds, readability tips — and why you should always add the text version too.