Regular Expressions Cheat Sheet with Practical Examples

8 min read · Published 1 November 2026 · Updated 2 November 2026

A regular expression (regex) describes a pattern of text so that a program can find, check or replace it. The syntax looks cryptic at first, but it is built from a small set of pieces. This cheat sheet follows the JavaScript flavour, which is what browsers and Node.js use, and shows each piece with a short example you can try in a regex tester.

Literals and special characters

Most characters match themselves: the pattern cat matches the text "cat". A few characters have a special meaning and must be escaped with a backslash to match literally: \ ^ $ . | ? * + ( ) [ ] { }. To match a literal dot, write \.

Character classes

  • . matches any single character except a line break (unless the s flag is set).
  • \d matches a digit, \w a letter, digit or underscore, \s whitespace. The uppercase versions (\D, \W, \S) match the opposite.
  • [abc] matches one of a, b or c. [a-z] is a range. [^abc] matches any character that is not a, b or c.

Anchors and boundaries

  • ^ matches the start of the text and $ the end (of each line when the m flag is set).
  • \b matches a word boundary, which is the position between a word character and a non-word character. \bcat\b matches "cat" but not "category".

Quantifiers

  • * means zero or more, + one or more, ? zero or one.
  • {3} means exactly three, {3,} three or more, {3,5} between three and five.
  • Quantifiers are greedy by default: they take as much text as they can. Add ? to make them lazy (*? and +?), so they take as little as possible.

Greedy versus lazy on "<b>one</b> and <b>two</b>"

<.+>     matches the whole string "<b>one</b> and <b>two</b>"
<.+?>    matches only "<b>"

Groups and alternation

  • (abc) is a capturing group. The text it matches can be read afterwards, for example as match[1].
  • (?:abc) groups without capturing, which is faster and keeps numbering clean.
  • (?<year>\d{4}) is a named capturing group, read as match.groups.year.
  • a|b matches a or b. Use a group to limit its reach: gr(a|e)y matches "gray" and "grey".
  • \1 or \k<year> refers back to what an earlier group matched, so \b(\w+)\s+\1\b finds repeated words.

Lookahead and lookbehind

Lookaround checks the text around a position without including it in the match.

  • X(?=Y) matches X only if Y follows. X(?!Y) matches X only if Y does not follow.
  • (?<=Y)X matches X only if Y comes before it. (?<!Y)X matches X only if Y does not come before it.
  • Example: \d+(?= USD) finds the number in "250 USD" and leaves " USD" out of the match.

Flags

  • g finds all matches instead of stopping at the first.
  • i ignores letter case.
  • m makes ^ and $ work on every line.
  • s lets . match line breaks.
  • u turns on full Unicode support, needed for characters outside the basic range and for \p{...} property escapes.
  • y (sticky) matches only at the exact position where the last match ended. The d flag additionally returns the start and end index of each group.

Practical patterns

Be careful with patterns for things like email addresses and URLs. A truly correct email regex is extremely long. A simple check for "something@something.something" is fine for catching typos in a form, but the only real proof that an address works is to send a message to it.

Common examples

Date 2026-11-01        (\d{4})-(\d{2})-(\d{2})
Hex colour #1a2b3c     #(?:[0-9a-fA-F]{3}){1,2}\b
Trim spaces            ^\s+|\s+$        (with the g flag)
Repeated word          \b(\w+)\s+\1\b
Positive integer       ^[1-9]\d*$

Avoiding catastrophic backtracking

Some patterns make the regex engine try an enormous number of ways to match, and a short input can freeze a page or a server. The classic cause is a quantifier nested inside another quantifier, such as (a+)+$ tested against a long string of a followed by an exclamation mark. To stay safe:

  • Avoid nested quantifiers on overlapping content.
  • Prefer specific classes ([^"]*) over broad ones (.*) where you can.
  • Anchor patterns when you can, and limit the length of input you run them on.
  • Treat patterns that process untrusted input with extra caution.

Using a regex in code

In JavaScript you can write a literal such as /\d+/g, or build one with new RegExp("\\d+", "g"). In a string, every backslash must be doubled, which is a frequent source of bugs. Test the pattern on realistic samples first, including empty strings and unusual input, and keep it readable with comments and named groups when it grows.

Building a pattern step by step

Start with the simplest version that matches your example, then tighten it. Suppose you want to find dates such as 2026-11-01 in a text:

  1. Start with the shape: \d{4}-\d{2}-\d{2} matches four digits, a hyphen, two digits, a hyphen and two digits.
  2. Add anchors if the whole string must be a date: ^\d{4}-\d{2}-\d{2}$. Without them the pattern also matches inside a longer string.
  3. Capture the parts you need: ^(\d{4})-(\d{2})-(\d{2})$, or with names, ^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$.
  4. Remember what a regex cannot do: it checks the shape, not the meaning. 2026-13-45 matches the pattern but is not a real date. Validate the values in code.

Replacing with capture groups

Captured groups can be reused in the replacement text. This turns an ISO date into a day, month, year format:

JavaScript

"2026-11-01".replace(/(\d{4})-(\d{2})-(\d{2})/, "$3/$2/$1")
// "01/11/2026"

Frequently asked questions

Are regex engines the same everywhere?
The basics are shared, but there are differences between flavours such as JavaScript, Python, Java and PCRE (used by PHP and many tools). Lookbehind support, named group syntax and Unicode handling vary, so test in the engine you will actually use.
Why does [A-Z] not match letters such as "ế" or "Đ"?
Ranges like [A-Z] cover only basic Latin letters. To match letters from any language, turn on the u flag and use the Unicode property \p{L}, for example /\p{L}+/gu. This is important for Vietnamese and other languages with accents.
How do I match across several lines?
Use the s flag so that the dot also matches line breaks, or use [\s\S] as a character class that matches any character. Use the m flag if you want ^ and $ to match at the start and end of each line.
Why does my regex work in the tester but not in my code?
Usual causes are a missing flag, unescaped backslashes in a string (write \\d inside a normal string), or a different regex engine. Copy the exact pattern and flags, and check how your language handles escaping.

Sources and further reading

Try it yourself: Regex Tester runs in your browser, free and with no sign-up.

More guides