Skip to main content
DeveloperAugust 6, 20264 min read

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.

#regex#regular expressions#javascript#developer tools#text processing
Regex for Beginners: Learn Regular Expressions with 10 Practical Examples

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

SymbolMatchesExample
abcThe exact text "abc"cat finds "cat" in "concatenate"
.Any character (except line break)c.t → cat, cut, c9t
\dA digit (0–9)\d\d → 42
\wA letter, digit or underscore\w+ → a word
\sA space, tab or line breaka\sb → "a b"
[abc]One of the listed charactersgr[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

SymbolMeaningExample
*0 or more timesab*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

SymbolMeaning
^Start of the text (or of the line with the m flag)
$End of the text (or of the line)
\bWord 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.

Regex Tester

Test your regular expressions in real time, groups included.

Try it for free

10 Practical Examples

  1. A whole number: ^\d+$
  2. A decimal number (with a dot or comma): ^\d+([.,]\d+)?$
  3. A simple email check: ^[^\s@]+@[^\s@]+\.[^\s@]+$
  4. A US ZIP code: ^\d{5}(-\d{4})?$
  5. A date in YYYY-MM-DD format: ^\d{4}-\d{2}-\d{2}$
  6. A time in 24h format: ^([01]\d|2[0-3]):[0-5]\d$
  7. A hex color: ^#([0-9a-fA-F]{3}){1,2}$
  8. A URL starting with http or https: ^https?://\S+$
  9. Double spaces to clean up: \s{2,} (replace with a single space)
  10. 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

Ready to use our dev tools ?

Free, fast, and no sign-up required.

Try it now

Related articles