本页面有中文版。
Cette page est disponible en français.
Esta página está disponible en español.
Guides1 live hunt inside

Regex for beginners: the first 10 patterns to learn

New to regular expressions? Start here. Ten small patterns, each one a building block: plain text, the dot, character classes, \d, quantifiers, anchors, alternation, groups, escaping and word boundaries. Every example is live, so you can edit it and see what changes.

Regex Hunter logo

· 9 min read

/^c.t$/

match

  • cat
  • cot
  • cut
  • c9t

spare

  • ct
  • coat
  • scat
  • cats

A regular expression, or regex, is a small pattern that describes text. "Three digits." "A word that starts with a capital." "Anything ending in .pdf." Code editors, spreadsheets, search tools and every programming language understand them.

They look cryptic because every character is short, not because the ideas are hard. Ten building blocks cover most of what people use day to day, and this post introduces them one at a time. Every example is live: change the pattern and the lines update.

01Plain text matches itself

Most characters just mean themselves. The pattern cat finds the letters c, a, t in a row, anywhere:

Matches5/5 as expected
/cat/

Should match3/3

  • cat
  • the cat sat
  • concatenate

Should not2/2

  • dog
  • CAT
Try it in the Playground
It matches "cat" inside other words too. Pattern 10 fixes that.

Matching is case-sensitive by default, so CAT doesn't count.

02The dot: any one character

. matches exactly one character, whatever it is:

Matches5/5 as expected
/c.t/

Should match3/3

  • cat
  • cut
  • c9t

Should not2/2

  • ct
  • coat
Try it in the Playground
One character, any character, between c and t.

ct fails because there's nothing between the c and the t. coat fails because there are two.

03A set of characters: [ ]

Square brackets mean "one character from this set":

Matches3/3 as expected
/gr[ae]y/

Should match2/2

  • grey
  • gray

Should not1/1

  • groy
Try it in the Playground
grey or gray, one character from the set [ae].

A hyphen inside makes a range: [a-z] is any lowercase letter, [0-9] any digit. More on sets in character classes.

04Shorthands: \d, \w, \s

Some sets have short names. \d is a digit, \w a word character (letter, digit or underscore), \s a space or other whitespace:

Matches4/4 as expected
/\d\d:\d\d/

Should match2/2

  • 09:30
  • meet at 14:05

Should not2/2

  • 9:30
  • noon
Try it in the Playground
Two digits, a colon, two digits: a time like 09:30.

05How many: + * ? {n}

A quantifier after something says how many times it repeats:

Quantifier Means
+ one or more
* zero or more
? zero or one (optional)
{3} exactly 3
{2,4} 2 to 4
Matches3/3 as expected
/colou?r/

Should match2/2

  • color
  • colour

Should not1/1

  • colr
Try it in the Playground
The ? makes the u optional, so both spellings match.
Matches2/2 as expected
/\d{3}-\d{4}/

Should match1/1

  • 555-0199

Should not1/1

  • 55-0199
Try it in the Playground
Exactly three digits, a hyphen, exactly four digits.

06Start and end: ^ and $

^ means "the start of the text" and $ means "the end". Use both when the whole text has to match, not just part of it:

Matches4/4 as expected
/^\d+$/

Should match2/2

  • 2026
  • 7

Should not2/2

  • 2026 AD
  • $20
Try it in the Playground
Only digits, from the very start to the very end.

Without them, \d+ would happily find the digits inside 2026 AD. More in anchors and multiline mode.

07Either-or: |

| means "or":

Matches4/4 as expected
/^(cat|dog)$/

Should match2/2

  • cat
  • dog

Should not2/2

  • bird
  • catdog
Try it in the Playground
Exactly cat, or exactly dog.

The parentheses keep the ^ and $ around both choices.

08Groups: ( )

Parentheses group several characters so a quantifier applies to all of them:

Matches4/4 as expected
/^(ha)+$/

Should match2/2

  • ha
  • hahaha

Should not2/2

  • haa
  • hah
Try it in the Playground
"ha" repeated one or more times. Without the group, + would only repeat the a.

09Escaping special characters: \

Characters like . ? + * ( and $ have special meanings. To match one literally, put a backslash before it:

Matches4/4 as expected
/^\d+\.\d\d$/

Should match2/2

  • 3.14
  • 19.99

Should not2/2

  • 3x14
  • 3.1
Try it in the Playground
\. is a real dot. A plain . would accept 3x14 too.

10Whole words: \b

\b marks the edge of a word. Wrap a word in \b … \b to match it on its own, not inside a longer word:

Matches5/5 as expected
/\bcat\b/

Should match3/3

  • cat
  • the cat sat
  • a cat.

Should not2/2

  • concatenate
  • cats
Try it in the Playground
cat as a whole word. Compare with pattern 1, which also matched "concatenate".

11Putting them together

A US-style phone number like 555-0199 or 555 0199: three digits, an optional hyphen or space, four digits, and nothing else:

Matches6/6 as expected
/^\d{3}[- ]?\d{4}$/

Should match3/3

  • 555-0199
  • 555 0199
  • 5550199

Should not3/3

  • 555--0199
  • 55-0199
  • call 555-0199
Try it in the Playground
Shorthand, quantifiers, a set, ? and both anchors in one short pattern.

That's pattern 3, 4, 5 and 6 working together. Most real patterns are just these blocks combined.

12Try one

Your first hunt: catch the monsters, spare the innocents.

Live huntpar 4

Catch the words that end in ing.

Targets · 0/5 caught

  • runningtarget
  • singtarget
  • thingtarget
  • bringtarget
  • kingtarget

Innocents · 0 hit

  • ingotinnocent
  • singerinnocent
  • angleinnocent
  • kingsinnocent
  • ring a bellinnocent

Type a regex — the board updates as you go.

Try it in the Playground
Like this? A new one drops every day.Play today’s hunt

13Checklist

  • Plain characters match themselves. . matches any one character.
  • [abc] is one character from a set. \d, \w, \s are shorthands.
  • + * ? {n} say how many.
  • ^ and $ pin the start and end.
  • | is "or". ( ) groups.
  • \ makes a special character literal. \b marks a word edge.

14Keep practising