DEV Community

pulkitgovrani
pulkitgovrani Subscriber

Posted on Originally published at ilovekit.app on

Regex Cheatsheet for Beginners (With Copy-Paste Examples)

Regular expressions look cryptic at first, but a small set of building blocks covers most real-world tasks: validating input, searching logs, and doing bulk find-and-replace. Learn these pieces and you can read and write most patterns you'll meet.

The basic building blocks

  • . matches any single character except a newline.
  • \d matches a digit, \w a word character (letters, digits, underscore), and \s whitespace. The capitals \D, \W, and \S mean the opposite.
  • [abc] matches any one of a, b, or c. [a-z] is a range, and [^abc] matches anything except those.
  • ^ and $ match the start and end of the string (or of each line with the m flag).
  • \b matches a word boundary, which is handy for whole-word matches.
  • Use a backslash to match a special character literally: \. matches a period, \( a parenthesis.

Quantifiers: how many times

  • * means zero or more, + means one or more, and ? means zero or one.
  • {3} means exactly 3, {2,5} means between 2 and 5, and {2,} means 2 or more.
  • Quantifiers are greedy by default and grab as much as they can. Add a ? after one (like .*?) to make it lazy so it matches as little as possible.
<.+>     // greedy: on "<b>hi</b>" matches the whole string
<.+?>    // lazy:   matches "<b>" first
Enter fullscreen mode Exit fullscreen mode

Groups, alternation, and backreferences

(cat|dog)s?                      // cat, cats, dog, dogs
(\d{4})-(\d{2})-(\d{2})         // capture year, month, day
(?<year>\d{4})-(?<month>\d{2})   // named groups
(?:abc)+                         // group without capturing
(\w)\1                           // a repeated character, like "ll" in "hello"
Enter fullscreen mode Exit fullscreen mode

Lookaheads and lookbehinds

Lookarounds check what surrounds a position without including it in the match.

\d+(?= dollars)      // digits followed by " dollars"
\d+(?! dollars)      // digits NOT followed by " dollars"
(?<=\$)\d+           // digits preceded by "$"
(?<!\$)\b\d+          // digits NOT preceded by "$"
Enter fullscreen mode Exit fullscreen mode

Flags

  • g finds all matches instead of stopping at the first.
  • i makes the match case-insensitive.
  • m makes ^ and $ match at line boundaries.
  • s lets . match newlines as well.

Practical patterns you can reuse

  • ISO date (format only): ^\d{4}-\d{2}-\d{2}$
  • Hex color: ^#(?:[0-9a-fA-F]{3}){1,2}$
  • Simple email shape: ^[^\s@]+@[^\s@]+\.[^\s@]+$
  • Trim extra spaces: \s{2,} replaced with a single space
  • Slug: ^[a-z0-9]+(?:-[a-z0-9]+)*$

Regex checks shape, not truth

The email pattern above accepts anything that looks like an address. Confirming an address actually exists means sending a message to it. Likewise, the date pattern accepts 2026-13-45; validate the values in code.

Common mistakes

  • Forgetting to escape special characters like . and ?, which then match far more than intended.
  • Using greedy .* and swallowing more text than you meant to.
  • Nested quantifiers such as (a+)+ that can cause catastrophic backtracking on certain inputs and freeze your program.
  • Assuming every engine behaves the same; lookbehind and named-group syntax differ between JavaScript, Python, and PCRE.

The fastest way to learn is to test against sample text and watch each match and group light up. Build the pattern one piece at a time.

Frequently asked questions

What does \d+ mean in regex?

\d matches a single digit and + means one or more, so \d+ matches a run of one or more digits, like 2026.

What is the difference between greedy and lazy quantifiers?

A greedy quantifier (.*) matches as much as possible; a lazy one (.*?) matches as little as possible. Lazy is often what you want when matching between delimiters.

How do I match a literal dot or bracket?

Escape it with a backslash: \. matches a period and \[ matches an opening bracket.

Is regex the right tool to validate an email address?

A simple shape check is fine for a form, but regex cannot prove an address exists. Send a confirmation email for real verification.

Try it: Regex Tester — free, runs in your browser, nothing is uploaded.

Originally published at ilovekit.app.

Top comments (0)