A regular expression (regex) is a search pattern. It can find every email address in a document, check that a phone number has the right format or replace dates from one style to another – in a single line. The syntax looks scary at first, so this guide to regex for beginners starts with the handful of symbols you will use most and builds up to practical examples.
What regex is for
- Validation – checking that input looks like an email, phone number or postcode
- Searching – finding patterns in logs, code or documents
- Replacing – reformatting text in bulk
- Extracting – pulling out numbers, links or IDs
Regular expressions work in almost every language (JavaScript, PHP, Python, Java), in editors like VS Code, and even in Google Sheets.
The essential symbols (cheat sheet)
Characters
.– any single character\d– a digit (0–9)\w– a word character (letter, digit or underscore)\s– whitespace (space, tab, new line)[abc]– any one of a, b or c[a-z]– any lower-case letter[^0-9]– anything except a digit
Quantifiers
*– zero or more+– one or more?– zero or one (optional){3}– exactly three{2,5}– between two and five
Anchors
^– start of the text (or line)$– end of the text (or line)\b– a word boundary
Groups and choices
(abc)– a group you can reuse or extractcat|dog– either cat or dog
To match a special character literally, escape it with a backslash: \. matches a real full stop.
Practical examples
A four-digit year:
\b\d{4}\b matches 2026 in "Founded in 2026".
A Nigerian mobile number:
^(\+234|0)[789][01]\d{8}$ matches 08031234567 and +2348031234567, but not 05031234567.
A simple email check:
^[^\s@]+@[^\s@]+\.[^\s@]+$ – something, then @, then something, a dot and something. Real email validation is complicated, so use this as a sanity check and confirm addresses by sending an email.
A date in DD/MM/YYYY format:
^\d{2}/\d{2}/\d{4}$
Extra spaces:
\s{2,} finds two or more spaces in a row – replace them with one space.
Flags
Flags change how a pattern behaves:
g– global: find all matches, not just the firsti– case-insensitivem– multiline: ^ and $ work on each line
Tips for writing patterns
- Start small and add pieces one at a time, testing as you go.
- Be specific.
.*matches almost anything, which often matches too much. - Use anchors for validation so the whole input must match, not just part of it.
- Comment complex patterns in your code – your future self will thank you.
Test patterns live
The free Regex Tester highlights matches as you type, shows capture groups and supports flags, so you can experiment safely before putting a pattern in your code.
Other developer helpers:
- JSON Formatter – see how to format JSON
- URL Parser – split links into parts without regex
- Remove Duplicate Lines – clean lists quickly
Frequently asked questions
Is regex the same in every language? The basics are, but advanced features vary slightly. Test in the flavour your code uses.
Should I validate emails with regex alone? No. Use a simple pattern to catch typos, then send a confirmation email.
Why is my pattern so slow?
Nested quantifiers such as (a+)+ can cause "catastrophic backtracking". Simplify the pattern.
What does a lazy quantifier do?
Adding ? after a quantifier, as in .*?, makes it match as little as possible. It is useful for extracting text between two markers, such as the contents of the first pair of quotes.
Summary
This regex for beginners guide covered the core symbols – character classes, quantifiers, anchors and groups – plus real patterns for years, phone numbers, emails and dates. Start small, anchor validation patterns and test as you build. Practise with the free Regex Tester.