Monu Tools

Regex Basics: Characters, Classes, Anchors, and Quantifiers

By Monu ToolsLast updated June 30, 2026

A regular expression is a small pattern language for describing text: not a fixed string to find, but a shape to match. Once you know a handful of building blocks, most everyday patterns become readable. This guide walks through those blocks with examples you can paste straight into the Regex Tester to watch them match in real time, in your browser.

Try the Regex Tester toolTest a regular expression against your text in real time. See every match highlighted, with capture groups and flags. Runs entirely in your browser.

Literal characters and metacharacters

Most characters in a pattern match themselves: the regex cat matches the letters c, a, t in sequence. The power comes from metacharacters, symbols with special meaning. The dot . matches any single character except a newline. Because these symbols are special, matching one literally means escaping it with a backslash: to match a real dot, you write a backslash followed by a dot. The metacharacters to know are . ^ $ * + ? ( ) [ ] { } | and the backslash itself.

Character classes: match one of a set

Square brackets define a character class that matches exactly one character from the set inside. [abc] matches a single a, b, or c. A hyphen makes a range, so [a-z] is any lowercase letter and [0-9] any digit. A caret just inside the brackets negates the class: [^0-9] matches any one character that is not a digit. Several classes are so common they have shorthands.

ShorthandMatchesEquivalent class
\dAny digit[0-9]
\wWord character (letter, digit, underscore)[A-Za-z0-9_]
\sWhitespace (space, tab, newline, and more)[ \t\n\r\f\v] and Unicode spaces
\D \W \SThe negation of each[^0-9] and so on

Anchors: match a position, not a character

Anchors do not consume any characters; they assert where you are in the text. The caret ^ matches the start of the string, and the dollar sign $ matches the end. A word boundary, written as a backslash and b, sits between a word character and a non-word character, which is how you match a whole word and not a fragment of a longer one.

Quantifiers: how many times

A quantifier says how many times the thing before it may repeat. The three symbols and one brace form cover almost everything:

  • * means zero or more. So a* matches an empty string, a, aa, and so on.
  • + means one or more. a+ needs at least one a.
  • ? means zero or one, making the previous token optional. colou?r matches both color and colour.
  • {n,m} means between n and m times. \d{3,5} matches three to five digits; {3} means exactly three, and {3,} means three or more.
\d{4}-\d{2}-\d{2}     matches an ISO date like 2026-06-30
[A-Za-z]+@[A-Za-z]+   a crude name@host shape (see the email guide for why crude)
^\s*$                 a line that is empty or only whitespace

Flags change the whole match

A regex carries flags that alter how the entire pattern behaves. The three you will reach for most are easy to remember.

FlagEffect
g (global)Find all matches, not just the first
i (ignore case)Match letters regardless of case
m (multiline)Make ^ and $ match at the start and end of each line, not just the whole string

Try it as you learn

The fastest way to internalise these blocks is to watch them light up against real text. The Regex Tester highlights every match as you type and runs entirely in your browser, so you can paste real data without it leaving your machine. For a thorough reference once you are past the basics, MDN's guide to regular expressions is the standard companion.

Test a pattern nowTest a regular expression against your text in real time. See every match highlighted, with capture groups and flags. Runs entirely in your browser.

Related articles