Tips Tutorials November 7, 2025 3 mins read

Regex Cheat Sheet: 60+ Copy-Paste Patterns (JS, Python, PCRE)

A practical regex cheat sheet with 60+ tested patterns for JavaScript, Python, and PCRE. Includes email, URL, date, number, and cleanup examples — with lookahead tips and testing tools.

Regular expressions are one of those tools you either love or fear. But once you get used to them, they’re magic — perfect for validation, cleanup, or quick debugging in code. Here’s a ready-to-use regex pack you can copy-paste for JavaScript, Python, and PCRE projects.

Quick Start: Flags & Syntax (JS / Python / PCRE)

JS:

const regex = /pattern/gi;

g = global, i = ignoreCase, m = multiline, s = dotAll, u = Unicode

Python:

import re
re.findall(r"pattern", text, flags=re.I | re.M)

PCRE / PHP:

preg_match_all('/pattern/im', $text);

Emails, URLs, IPs, Phones

Email:

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}

URL (HTTP/S):

https?:\/\/(www\.)?[\w-]+(\.[\w-]+)+[/#?]?.*

IPv4:

\b(?:\d{1,3}\.){3}\d{1,3}\b

Phone (international):

\+?\d{1,3}?[-.\s]?\(?\d+\)?[-.\s]?\d+[-.\s]?\d+

Dates & Times, Numbers & Currency

ISO Date (YYYY-MM-DD):

\d{4}-\d{2}-\d{2}

US Date (MM/DD/YYYY):

(0?[1-9]|1[0-2])[\/\-](0?[1-9]|[12]\d|3[01])[\/\-]\d{4}

24h Time:

([01]\d|2[0-3]):[0-5]\d

Number with commas or decimals:

\d{1,3}(,\d{3})*(\.\d+)?

Currency ($1,234.56):

\$?\d{1,3}(,\d{3})*(\.\d{2})?

Text Cleanup

Remove extra whitespace:

\s{2,}

(Replace with single space)

Trim spaces from start/end (JS):

text.replace(/^\s+|\s+$/g, '')

Remove duplicate words:

\b(\w+)\s+\1\b

Keep only letters & numbers:

[^a-zA-Z0-9]+

Lookaheads & Lookbehinds: Practical Uses

Positive lookahead: match “cat” only if followed by “fish”

cat(?=fish)

Negative lookahead: match “cat” not followed by “fish”

cat(?!fish)

Positive lookbehind (Python/PCRE): match “fish” only if preceded by “cat”

(?<=cat)fish

Negative lookbehind:

(?<!cat)fish

Practical case:

  • Match numbers not in brackets: (?<!\[)\d+(?!\])

  • Match .js files only in /src/: (?<=\/src\/)[^\/]+\.js

Testing Tips + Online Playground

Use these tools to test your patterns:

✅ Always test with multiple examples
✅ Use flags (gimuy) carefully
✅ Keep patterns readable — break long ones with comments if supported

FAQ

Q: What are lookaheads vs lookbehinds?
A: Lookaheads check text after your match; lookbehinds check before. They don’t consume characters — they just assert conditions.

Q: How to make a regex Unicode-safe?
A: Use the u flag (JS) or re.UNICODE (Python). Example: /\p{L}+/u matches any Unicode letter.

Q: When NOT to use regex?
A: Avoid it for full HTML parsing, natural-language processing, or nested data (like JSON/XML). Use proper parsers instead.

Ending note:

Regex isn’t about memorizing patterns — it’s about recognizing structure. The more you test and tweak, the more fluent you get. Bookmark this cheat sheet, and next time you see a messy string, you’ll smile instead of panic.

📚 More JavaScript Snippets You’ll Love

If you found these regex patterns useful, check out our other quick-reference packs:

👉 50 JavaScript Snippets You’ll Reuse in 2025 — real-world code tricks for arrays, dates, async, and DOM.
👉 Top 30 Must-Know JavaScript Snippets for Developers — essential one-liners every dev should have ready to copy.

Each post is short, practical, and built for everyday coding. Save them for your next project! 🚀

David Green

David Green