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.
Worked example: greedy against lazy
Take the text <b>bold</b> and <i>it</i> and the pattern <.+>. With the g flag, you might expect four tags. Instead you get one match: the whole text from the first < to the last >. The dot plus is greedy. It runs to the end of the line, then backs up until a > can match, which is the very last one.
Change it to <.+?> and the lazy quantifier stops at the first > it can. With the g flag you now get four matches: <b>, </b>, <i> and </i>.
A third option is <[^>]+>, which says a tag is a < followed by any characters that are not >, then a >. It gives the same four matches and avoids the backtracking that a lazy dot needs, so it is often the better choice.
Quick reference table
| Pattern | Applied to | Result |
|---|---|---|
| \d+ | order 12, order 345 | 12 and 345 with the g flag |
| ^\d{10}$ | 9876543210 | Matches |
| ^\d{10}$ | 98765 43210 | No match, because of the space |
| \s{2,} | a b c | Finds the two runs of spaces, so replacing with one space gives a b c |
| ^line\d$ | two lines, line1 then line2 | No match without the m flag, both lines with it |
| \$(\d+(?:\.\d+)?) | price: $45.50 and $3 | $45.50 and $3, with the number captured in group 1 |
| (?<=foo)\d+ | foo123bar | 123, only because foo comes before it |
The last row uses a lookbehind. It checks what comes before the match without including it in the result. Lookbehind is supported in current JavaScript engines but not in some older browsers.
Reformatting a date with named groups
Named groups make replacements readable. Apply (?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2}) to 2026-11-03. The groups hold y as 2026, m as 11 and d as 03. In a replacement string, $<d>/$<m>/$<y> rebuilds the text as 03/11/2026.
Note that this only changes the order of the text. It does not check that the date is real, so 2026-99-99 matches too. Validate the value in code after the pattern has split it up.
Frequently asked questions
- How do I match a literal dot or bracket? Put a backslash before it, as in \. or \[. Inside square brackets most characters are already literal, so [.] also matches a dot.
- What is the difference between * and +? A star allows zero repeats, so ab* matches a on its own. A plus needs at least one, so ab+ needs at least one b.
- Why does ^ and $ not work on each line? Without the m flag they match only at the start and end of the whole text. Add m to match at every line.
- How do I make a match ignore case? Add the i flag. Then cat matches Cat and CAT.
- Why is my pattern slow or freezing? Nested quantifiers like (a+)+ can force the engine to try an enormous number of paths on input that almost matches. Rewrite the pattern to be more specific, or limit the input length.