Regular Expressions for Beginners: A Practical Regex Cheat Sheet
Regular expressions (regex) look like line noise at first: ^\d{3}-\d{4}$. But a small set of building blocks covers most everyday needs: validating input, searching logs, extracting data, and find-and-replace across text. The fastest way to learn is to try patterns on real text and see what matches.
Test as you learn
The Regex Tester highlights every match live, shows groups and named groups, and the result of a replace. It uses your browser's own regex engine (JavaScript), so results match what your web code will do.
The building blocks
Literal characters
cat matches "cat" anywhere: in "cat", "concatenate" and "bobcat".
Character classes
[aeiou]any one vowel.[0-9]any digit;[a-z]any lowercase letter.[^0-9]any character that's not a digit.\ddigit,\wword character (letter, digit, underscore),\swhitespace..any character except a line break.
Quantifiers
*zero or more,+one or more,?zero or one.{3}exactly three,{2,4}two to four,{2,}two or more.
\d+ matches one or more digits: "7", "42", "2026".
Anchors
^start of the line (or text),$end.\bword boundary.
\bcat\b matches "cat" as a whole word, not inside "concatenate".
Groups and alternatives
(abc)groups and captures;(?:abc)groups without capturing.(?<year>\d{4})a named group.cat|dogeither one.
Escaping
Characters with special meaning (. * + ? ( ) [ ] { } ^ $ | \) need a backslash to be matched literally: \. matches a full stop.
Flags
gglobal: find every match, not just the first. If your pattern finds only one match, turn ong.iignore case.mmultiline:^and$match at each line.
Practical examples
- Whole word "error":
\berror\b - A year from 1900 to 2099:
\b(19|20)\d{2}\b - An ISO date like 2026-10-18:
\d{4}-\d{2}-\d{2} - Spaces at the start or end of a line:
^\s+|\s+$(with themflag) - Repeated words like "the the":
\b(\w+)\s+\1\b - A simple email shape:
[^\s@]+@[^\s@]+\.[^\s@]+
Test each one in the tester with your own sample text.
A word about email and URL validation
Fully validating email addresses with regex is famously complicated. For forms, a simple shape check like the one above plus a confirmation email is far more reliable than a monster pattern.
Find and replace with regex
Regex shines in find-and-replace. In Find and Replace, turn on regular expressions and use groups in the replacement:
- Find
(\d{2})/(\d{2})/(\d{4})and replace with$3-$2-$1to turn18/10/2026into2026-10-18.
Common pitfalls
- Greedy matching:
<.*>matches from the first<to the last>. Use<.*?>for the shortest match. - Forgetting anchors:
\d{4}also matches inside "123456". - Different engines: JavaScript, Python, PCRE and others differ in details. Test in the engine you'll use.
Lookarounds, briefly
Sometimes you want to match something only when it's next to something else, without including that neighbour in the match:
\d+(?=%)digits followed by a percent sign: matches "25" in "25%".\d+(?![\d%])digits not followed by a percent sign (the\dinside stops it from matching just the "2" of "25%").(?<=₹)\d+digits preceded by a rupee sign: matches "500" in "₹500".(?<!-)\b\d+numbers not preceded by a minus sign.
Lookbehind ((?<=…) and (?<!…)) works in current browsers' JavaScript; some older engines and languages limit it.
Common questions
Is my text uploaded? No. Everything happens in your browser, and the page is blocked from uploading anything.
Can regex parse HTML? For quick searches, yes; for real parsing, use an HTML parser.
Summary
- Classes, quantifiers, anchors and groups cover most needs.
- Use
gfor all matches,\bfor whole words. - Test with real text before using a pattern in code.