Regex Cheatsheet

Interactive regex reference with examples

Regex Cheatsheet is a free interactive reference for regular expressions. Look up character classes, quantifiers, anchors, groups, and lookaround assertions with live examples you can test in the browser. Covers JavaScript, Python, and common validation patterns.

Live Regex Tester

Enter a pattern to test

Reference

Character classes match specific sets of characters. Use brackets for custom sets.
PatternDescriptionExample
.Any character except newlinea.c → "abc", "a1c"
\dDigit [0-9]\d\d → "42", "99"
\DNon-digit\D+ → "abc"
\wWord char [a-zA-Z0-9_]\w+ → "hello_123"
\WNon-word char\W → "!", "@"
\sWhitespace\s+ → " ", "\t"
\SNon-whitespace\S+ → "hello"
[abc]Any of a, b, or c[aeiou] → vowels
[^abc]Not a, b, or c[^aeiou] → consonants
[a-z]Range a to z[a-z]+ → lowercase
Quantifiers specify how many times a pattern should match. Add ? for lazy matching.
PatternDescriptionExample
*0 or moreab*c → "ac", "abc", "abbc"
+1 or moreab+c → "abc", "abbc"
?0 or 1colou?r → "color", "colour"
{n}Exactly n times\d{4} → "2024"
{n,}n or more times\d{2,} → "42", "123"
{n,m}Between n and m times\d{2,4} → "42", "123"
*?0 or more (lazy)<.*?> → shortest match
+?1 or more (lazy)\".+?\" → shortest string
Anchors match positions, not characters. Use them to match at specific locations.
PatternDescriptionExample
^Start of string/line^Hello → starts with "Hello"
$End of string/lineWorld$ → ends with "World"
\bWord boundary\bcat\b → "cat" not "cats"
\BNon-word boundary\Bcat → "cats" not "cat"
Groups capture parts of the match. Use them for extraction or backreferences.
PatternDescriptionExample
(abc)Capturing group(\d+)-(\d+) → captures parts
(?:abc)Non-capturing group(?:Mr|Mrs) Smith
\1, \2Backreference(\w)\1 → doubled letters
(?<name>abc)Named capturing group(?<year>\d{4})
a|bAlternation (or)cat|dog → "cat" or "dog"
Lookaround assertions match a position based on what comes before or after, without including it in the match.
PatternDescriptionExample
(?=abc)Positive lookahead\d+(?=px) → number before "px"
(?!abc)Negative lookahead\d+(?!px) → not before "px"
(?<=abc)Positive lookbehind(?<=\$)\d+ → after "$"
(?<!abc)Negative lookbehind(?<!\$)\d+ → not after "$"
Ready-to-use patterns for common validation tasks. Click "Try" to test or copy for your project.
Use CasePattern
Email (simple)[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
URLhttps?://[^\s]+
Phone (US)\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
IP Address (IPv4)\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Date (YYYY-MM-DD)\d{4}-\d{2}-\d{2}
Time (HH:MM)\d{1,2}:\d{2}(?::\d{2})?
Hex Color#[0-9A-Fa-f]{3,6}\b
Username^[a-zA-Z][a-zA-Z0-9_]{2,15}$
Strong Password^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
Slug^[a-z0-9]+(?:-[a-z0-9]+)*$

JavaScript

/pattern/flags str.match(regex) str.replace(regex, new) regex.test(str)

Python

import re re.search(pattern, str) re.findall(pattern, str) re.sub(pattern, new, str)

Flags

g - global (all matches) i - case insensitive m - multiline (^ $ per line) s - dotall (. matches \n)

About This Regex Cheatsheet

Regular expressions are a pattern language built into virtually every programming language and text editor. They let you describe the shape of text — "three digits followed by a dash" or "a word at the start of a line" — so a computer can find, extract, or replace matching fragments automatically.

This cheatsheet covers six categories of regex syntax: character classes (\d, \w, [a-z]), quantifiers (*, +, {n,m}), anchors (^, $, \b), groups and backreferences, lookaround assertions, and ready-to-use patterns for common tasks like email, URL, and date validation.

Every pattern in the reference table has a Try button that loads it into the live tester above so you can see matches highlighted in real time. You can also modify the pattern, toggle flags, and paste your own test strings.

Worked Examples

1. Extract all prices from a receipt.
Pattern: \$\d+\.\d{2}
Test string: Coffee $4.50, Bagel $3.25, Tax $0.62
Result: 3 matches — $4.50, $3.25, $0.62

2. Validate a date in YYYY-MM-DD format.
Pattern: ^\d{4}-\d{2}-\d{2}$
Test string: 2024-01-15 → match. 15/01/2024 → no match.
This pattern checks structure only. To verify that month ≤ 12 and day ≤ 31, add application logic.

3. Replace duplicate whitespace.
Pattern: \s{2,} with replacement   (single space).
Input: hello    world → Output: hello world.
In JavaScript: str.replace(/\s{2,}/g, ' ')

When to Use Regex

  • Search and replace — find patterns in log files, code, or documents
  • Input validation — check format of emails, phone numbers, dates, or codes
  • Data extraction — pull structured pieces (prices, IDs, timestamps) from unstructured text
  • Text cleanup — normalize whitespace, strip HTML tags, fix formatting

Limitations

Regex cannot parse nested structures like HTML or JSON reliably. For those, use a proper parser. Regex engines also differ between languages — JavaScript lacks some features available in Python or Perl (e.g., atomic groups, conditional patterns). Always test patterns against edge cases before deploying to production.

Frequently Asked Questions

Privacy

All regex testing runs locally in your browser using JavaScript. No patterns, test strings, or match results leave your device. Nothing is logged, stored, or sent to any server. There is no account, no tracking, and no cookies.

Related Tools

Simple Regex Tester
Test a regex pattern against sample text with match highlighting
JSON Formatter
Format and beautify JSON with proper indentation
Hash Generator
Generate MD5, SHA-1, SHA-256, and SHA-512 hashes
JWT Decoder
Decode JWT tokens and display header and payload
Markdown Preview
Live preview of Markdown rendering
Cron Schedule Validator
Validate 5-field cron expressions quickly
Request a New Tool
Improve This Tool