RegexProgrammingTutorialDeveloper

How to Use Regex: A Beginner's Complete Guide with Examples

Regular expressions seem cryptic, but they follow simple rules. Learn regex from scratch with practical examples you can test right in your browser.

BuiltItDev Team·May 28, 2026·10 min read
How to Use Regex: A Beginner's Complete Guide with Examples

What is a regular expression?

A regular expression (regex) is a pattern that describes a set of strings. Think of it as a search query on steroids — instead of searching for an exact word, you search for apattern that can match thousands of variations.

For example, the regex /\d3-\d4/matches any string like "555-1234" — three digits, a dash, four digits. You didn't need to list every possible phone number; the pattern handles all of them.

The building blocks

Every regex is built from a handful of core concepts. Master these and you can read (and write) most patterns you'll encounter.

Literal characters

The simplest regex is just text: /hello/matches the word "hello" exactly. Most characters match themselves — letters, digits, spaces.

The dot (.) — match anything

A dot matches any single character except a newline. /h.t/matches "hat", "hit", "hot", and even "h9t".

Character classes ([])

Square brackets define a set of allowed characters. /[aeiou]/ matches any single vowel. Add a caret to negate: /[^aeiou]/ matches any non-vowel.

Shorthand classes

PatternMeaningEquivalent
\dAny digit[0-9]
\wWord character[a-zA-Z0-9_]
\sWhitespace[ \t\n\r]
\DNon-digit[^0-9]
\WNon-word[^a-zA-Z0-9_]
\SNon-whitespace[^ \t\n\r]

Quantifiers — how many?

Quantifiers control how many times a token can repeat:

  • * — zero or more: /ab*c/ matches "ac", "abc", "abbc"
  • + — one or more: /ab+c/ matches "abc", "abbc" but NOT "ac"
  • ? — zero or one: /colou?r/ matches "color" and "colour"
  • {n} — exactly n: /\d{3}/ matches exactly 3 digits
  • {n,m} — between n and m: /\d{2,4}/ matches 2 to 4 digits

Anchors — where to match

Anchors don't match characters — they match positions:

  • ^ — start of string: /^Hello/ only matches "Hello" at the beginning
  • $ — end of string: /world$/ only matches "world" at the end
  • \b — word boundary: /\bcat\b/ matches "cat" but not "concatenate"

Groups and alternation

Parentheses () create capture groups. Use | for alternation (logical OR):

/(cat|dog|bird)s?/
// Matches: "cat", "cats", "dog", "dogs", "bird", "birds"

Capture groups also let you extract matched text. In JavaScript:

const match = "2026-05-29".match(/(\d{4})-(\d{2})-(\d{2})/)
// match[1] = "2026", match[2] = "05", match[3] = "29"

Common patterns you'll use daily

PatternMatches
/^\S+@\S+\.\S+$/Basic email validation
/^https?:\/\/.+/URL starting with http/https
/^\d{1,3}(\.\d{1,3}){3}$/IPv4 address format
/^#?[0-9a-fA-F]{6}$/Hex color code
/^\+?\d{10,15}$/Phone number (digits only)
Pro tip
Don't memorise every regex pattern. Instead, learn the building blocks and combine them. Use a live regex tester to experiment and verify your patterns against real data before putting them in code.

Flags you should know

Flags modify how the regex engine works:

  • g (global) — find all matches, not just the first
  • i (case-insensitive) — /hello/i matches "Hello", "HELLO"
  • m (multiline) — ^ and $ match line starts/ends, not just string start/end
  • s (dotAll) — . matches newlines too

Try it yourself

The fastest way to learn regex is to experiment. Open our Regex Tester & Explainer, paste a pattern, and see matches highlight in real time. Try modifying the examples above and watch how the matches change. There's no substitute for hands-on practice.


Regular expressions aren't magic — they're a small language with simple rules. Once you learn the building blocks, you can read any regex. And once you can read them, you can write them. Start with literal text, add character classes, sprinkle quantifiers, and anchor to positions. That's 90% of regex right there.