Home › Developer Tools › Regex Cheat Sheet

Regex Cheat Sheet

A searchable library of tested regular expressions for everyday tasks, from validating emails and IP addresses to extracting hashtags and URLs. Every pattern has its own test box that highlights matches live, plus a quick syntax reference.

Validation patterns use the m flag so you can test one value per line; drop it to validate a single string. Matches are highlighted live as you edit the test text.

Regex syntax quick reference

TokenMeaningExample
Advertisement
Advertisement

How to use the Regex Cheat Sheet

  1. Search for what you need, such as email, date or ipv4, or pick a category tab.
  2. Read the description to see what the pattern accepts and where its limits are.
  3. Edit the test text on the card. Matches are highlighted as you type and the count updates; press Test to re-run.
  4. Copy the regex as a JavaScript literal or as the bare pattern for other languages.
  5. Check the syntax reference at the bottom when you want to adapt a pattern, then download the cheat sheet for offline use.

Regex flags explained

FlagNameEffectExample
gglobalFind all matches instead of stopping at the first"a1b2".match(/\d/g) → ["1", "2"]
iignore caseLetters match regardless of case/hello/i matches "HeLLo"
mmultiline^ and $ match at every line break/^\d+$/m checks each line
sdotAll. also matches line breaks/a.b/s matches "a\nb"
uunicodeTreats surrogate pairs as one character and enables \p{…}/\p{L}+/u matches "Zürich"
ystickyMatches only at lastIndex, useful for tokenizers/\d/y
dindicesAdds start and end positions of each group to the result/(a)/d.exec("ba").indices
vunicodeSetsUpgraded u mode with set operations in classes/[\p{L}--[a-z]]/v

Greedy vs lazy: a worked example

Given the text <b>bold</b> and <i>italic</i>:

  • /<.+>/ is greedy: .+ runs to the end of the string, then backtracks to the last >, so the single match is the whole text, 30 characters.
  • /<.+?>/g is lazy: .+? stops at the first >, giving four matches: <b>, </b>, <i>, </i>.
  • /<[^>]+>/g gives the same four matches without backtracking, because the negated class can never run past a >. Negated classes are usually the fastest and clearest choice.

Using these patterns in code

LanguageValidate a whole stringFind all matches
JavaScript/^\d{5}$/.test(zip)[...text.matchAll(/#\w+/g)]
Pythonre.fullmatch(r"\d{5}", zip)re.findall(r"#\w+", text)
PHPpreg_match('/^\d{5}$/', $zip)preg_match_all('/#\w+/', $text, $m)
Javazip.matches("\\d{5}")Pattern.compile("#\\w+").matcher(text)
C#Regex.IsMatch(zip, @"^\d{5}$")Regex.Matches(text, @"#\w+")
Goregexp.MustCompile(`^\d{5}$`).MatchString(zip)re.FindAllString(text, -1)

Tip: in languages where patterns are written as normal strings (Java, C++), every backslash must be doubled. Raw strings (r"…" in Python, @"…" in C#, backticks in Go) avoid that.

Frequently asked questions

Do these regex patterns work outside JavaScript?

Most do. The patterns use widely supported syntax that also works in Python, Java, C#, PHP (PCRE) and Go with small changes. Lookbehinds such as (?<!\w) are not supported in Go's RE2 engine, and \p{Extended_Pictographic} needs a Unicode-aware engine. Remove the slashes and flags when you use the bare pattern.

What is the best regex for email addresses?

There is no perfect one; the full RFC 5322 grammar is too complex for a practical regex. The pattern here accepts normal addresses and rejects obvious mistakes such as a missing @ or a domain without a dot. To be sure an address exists, send a confirmation email.

Why do the validation patterns use the m flag?

So that each line of the test box is checked as a separate value. With the m flag, ^ and $ match at the start and end of every line. When you validate one input field in your code, drop the m flag so the whole string must match.

Can a regex validate dates fully?

Only partly. The date patterns check the format and the ranges of months and days, but not month lengths or leap years, so 2026-02-30 still passes. Parse the date in code after the regex check.

What is catastrophic backtracking?

Some patterns with nested quantifiers, like (a+)+, can take exponential time on certain non-matching input, freezing a page or server (a ReDoS). The patterns on this page avoid ambiguous nesting, but always test custom patterns against long, nearly matching strings.

Is my test text sent anywhere?

No. All matching runs in your browser with the built-in JavaScript RegExp engine. Nothing you type is uploaded.

Related tools