Regular expressions look like a cat walked across the keyboard, but almost every real-world pattern is built from the same handful of pieces. Learn these eight and you can read and write the regex you’ll actually meet — validating input, finding text, cleaning data.
Short answer: Master classes, anchors, quantifiers, and groups, then build up. Paste any pattern below into the Regex Tester to watch it match live, with highlighting and capture groups.
1. Literals and escaping
Most characters match themselves: cat matches “cat”. But some are special — . * + ? ( ) [ ] { } ^ $ | \ — and to match them literally you put a backslash in front: \. matches a real dot, \$ a real dollar sign.
2. Character classes: [ ]
A class matches one character from a set.
[aeiou]— any vowel[a-z]— any lowercase letter;[A-Za-z0-9]— any letter or digit[^0-9]— any character that is not a digit (the^inside brackets means “not”)
Shorthands save typing: \d = a digit, \w = a word character (letter, digit, underscore), \s = whitespace. Their capitals invert them — \D is any non-digit.
3. Anchors: ^ $ \b
Anchors match a position, not a character.
^— start of the string (or line, with themflag)$— end of the string (or line, with themflag)\b— a word boundary, e.g.\bcat\bmatches “cat” but not “category”
^\d{5}$ means “the whole string is exactly five digits” — a classic US ZIP check.
4. Quantifiers: * + ? { }
Quantifiers say how many of the previous thing.
| Pattern | Meaning |
|---|---|
* |
zero or more |
+ |
one or more |
? |
zero or one (optional) |
{3} |
exactly three |
{2,4} |
between two and four |
{2,} |
two or more |
Add a ? to make any of them lazy (match as little as possible): ".+?" grabs one quoted string instead of everything between the first and last quote.
5. Groups: ( )
Parentheses group parts of a pattern and capture what they match so you can reuse it.
(ab)+— one or more repeats of “ab”(\d{4})-(\d{2})-(\d{2})— captures year, month, day from a date as groups$1,$2,$3(?:...)— a non-capturing group when you only need the grouping, not the value
6. Alternation: |
The pipe means or: cat|dog|fish matches any of the three. Combine with groups to scope it: ^(GET|POST|PUT)\b matches a line that begins with one of those HTTP methods (add the m flag to catch every line, not just the first).
7. The dot: .
. matches any single character except a newline. So a.c matches “abc”, “a2c”, “a-c”. Need it to include newlines? Turn on the s (dotall) flag. And remember: to match a literal period, escape it — \..
8. Flags
Flags change how the whole pattern behaves:
g— global: find all matches, not just the firsti— case-insensitivem— multiline:^and$match at every line breaks— dotall:.also matches newlines
Two you’ll reach for less often: u turns on full Unicode handling, and y (sticky) matches only at the current position. The Regex Tester exposes all six as toggles.
Putting it together
A few patterns you can lift and adapt:
- Hashtags:
#(\w+)→ captures each tag after the# - Simple email shape:
[\w.+-]+@[\w-]+\.[\w.-]+ - Repeated whitespace to one space: replace
\s+with a single space - A 24-hour time:
([01]\d|2[0-3]):[0-5]\d
Drop any of these into the Regex Tester, paste some sample text, and watch each match light up — including the capture groups. It runs matching safely off the main thread, so even an accidental slow pattern won’t freeze the page while you experiment.
Where regex shines next
Once you can match, you can clean and extract. Pair it with the JSON Formatter when you’re picking apart API payloads, or the Character Counter when you’re checking that cleaned-up text fits a length limit. Start small, test as you go, and the cat-on-keyboard look stops being intimidating.