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
s (dotAll)Let . match newlines too, so a pattern can span lines
u (unicode)Treat the pattern as Unicode code points and enable \p{...} property escapes

Flags combine, and order does not matter: /cat/gi and /cat/ig behave the same. One subtlety catches people out: the g flag makes a regex object stateful. When you call the same /pattern/g repeatedly with .test() or .exec(), it remembers a lastIndex between calls, so a second .test() on the same string can return false. If you only need to match, matchAll or a fresh pattern per call sidesteps that.

Common mistakes to sidestep

A handful of misunderstandings account for most broken beginner patterns. Each has a concrete fix.

  • Metacharacters go quiet inside a class. Inside [ ], most special symbols are literal: [.] matches a real dot and [+*?] matches those punctuation marks, no escaping needed. Only ^ (at the start), - (between two characters), ] and the backslash keep special meaning there.
  • The dot is narrower than you think. . matches any character except a line break, so it will not cross a newline unless you add the s flag. It is also overused: to match a literal dot in a domain or filename, write \. rather than a bare dot that matches anything.
  • \d and \w are ASCII in JavaScript. \d is exactly [0-9] and \w is exactly [A-Za-z0-9_], even with the u flag. Neither matches accented letters or non-Latin digits, so naive \w+ silently drops names like Zoe with an accent. Use an explicit class or a Unicode property escape when that matters.
  • Forgetting g on a replace. String.replace with a plain pattern changes only the first match; you need the g flag (or replaceAll) to change every one.
  • ^ and $ mean whole string by default. Without the m flag they anchor to the ends of the entire input, not each line, which surprises people validating multi-line text.
[.]        matches a literal dot (no backslash needed inside a class)
\.         matches a literal dot outside a class
[a-z-]     the trailing - is literal, so this is a-z or a hyphen
\bZoe\b    \w-based word matching skips the accented Zoe unless you widen the class

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. Once these blocks click, the natural next steps are capturing groups, greedy and lazy quantifiers, and lookahead and lookbehind. 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.

Sources

Related articles