Regex Cheat Sheet with Practical Examples
The regular expression syntax you actually use, with copy-ready patterns for emails, dates, numbers and cleanup, and the mistakes that make a pattern match too much or too little.
What a regular expression is
A regular expression, or regex, is a pattern that describes a set of strings. You use it to find text, check that input has the right shape, or pull pieces out of a larger string. The same idea exists in almost every language, but the details differ a little. The Regex Tester runs the JavaScript flavour, so a pattern that works there will work in browser code and Node.js.
The building blocks
Most patterns are built from a small set of pieces.
- Literal characters match themselves. The pattern cat matches the text cat.
- A dot matches any single character except a line break.
- \d matches a digit, \w matches a letter, digit or underscore, and \s matches whitespace. The capital forms \D, \W and \S match the opposite.
- Square brackets match one character from a set. [aeiou] matches a vowel and [a-z0-9] matches a lowercase letter or digit. A caret at the start, as in [^0-9], means anything except those characters.
- ^ matches the start of the text and $ matches the end. \b matches the edge of a word.
- A pipe means or. The pattern cat|dog matches either word.
Quantifiers: how many times
A quantifier goes after a piece and says how often it may repeat. A star means zero or more, a plus means one or more, and a question mark means zero or one. Curly braces give exact counts: \d{4} is exactly four digits and \d{2,4} is two to four.
Quantifiers are greedy by default, which means they take as much text as they can. Adding a question mark after one, as in .+?, makes it lazy so it takes as little as possible. This difference is behind most surprising matches, such as <.+> grabbing everything from the first tag to the last tag on a line instead of one tag.
Groups and captures
Round brackets group part of a pattern and remember what they matched. In (\w+)@(\w+)\.com applied to alice@example.com, the first group holds alice and the second holds example. Groups let you apply a quantifier to several characters at once, as in (ab)+, and they let you reuse the pieces when you replace text.
If you only need grouping and not the captured text, write (?:...) instead. Named groups, written (?<year>\d{4}), make longer patterns easier to read.
Flags
Flags change how the whole pattern behaves. The g flag finds every match instead of only the first. The i flag ignores case. The m flag makes ^ and $ match at each line instead of only at the ends of the whole text. The s flag lets the dot match line breaks. The Regex Tester has a separate field for flags, and g is on by default.
Patterns you can start from
Treat these as starting points and test them against your own data.
- Simple email shape: [\w.+-]+@[\w-]+\.[\w.-]+. It checks the shape only, not whether the address exists.
- Date such as 2026-11-03: \d{4}-\d{2}-\d{2}.
- Whole number or decimal: -?\d+(\.\d+)?
- Extra spaces to remove: \s{2,}
- Hex colour: #[0-9a-fA-F]{3,8}\b
- Text between double quotes: "([^"]*)"
Mistakes that cost the most time
Forgetting to escape special characters is the most common one. A literal dot must be written \., otherwise it matches any character, so example.com also matches exampleXcom. The same applies to ( ) [ ] { } + * ? | ^ and $.
Another is writing a pattern that is too loose. A check for a phone number such as \d+ passes any run of digits. Anchor it with ^ and $ and state the length you expect.
Last, avoid nested quantifiers such as (a+)+ on long input. They can make the engine try an enormous number of combinations, which freezes a page or a server. Keep patterns as specific as you can, and test them with both text that should match and text that should not.