← Back to Blog

Regular Expressions Cheat Sheet for Developers

March 9, 2026 9 min read By CodeTidy Team

Regular expressions are one of the most powerful tools in a developer's toolkit — and one of the most frustrating to learn. This cheat sheet covers the syntax you'll actually use, with practical examples for every pattern.

Basic Matchers

PatternMatchesExample
.Any character except newlinea.c matches abc, a1c, a-c
\dAny digit (0-9)\d\d\d matches 123
\DAny non-digit\D+ matches abc
\wWord char (a-z, A-Z, 0-9, _)\w+ matches hello_world
\WNon-word character\W matches @, !, space
\sWhitespace (space, tab, newline)\s+ matches
\SNon-whitespace\S+ matches hello

Anchors

PatternMatchesExample
^Start of string/line^Hello matches Hello world but not Say Hello
$End of string/lineworld$ matches Hello world
\bWord boundary\bcat\b matches cat but not category
\BNon-word boundary\Bcat\B matches the cat in location

Quantifiers

PatternMeaningExample
*0 or moreab*c matches ac, abc, abbc
+1 or moreab+c matches abc, abbc but not ac
?0 or 1 (optional)colou?r matches color and colour
{3}Exactly 3\d{3} matches 123
{2,4}Between 2 and 4\d{2,4} matches 12, 123, 1234
{2,}2 or morea{2,} matches aa, aaa, aaaa...

Greedy vs. lazy: By default, quantifiers are greedy (match as much as possible). Add ? to make them lazy (match as little as possible):

// Greedy: .* matches everything
"<div>hello</div><div>world</div>".match(/<div>.*<\/div>/)
// Matches: "<div>hello</div><div>world</div>"

// Lazy: .*? matches as little as possible
"<div>hello</div><div>world</div>".match(/<div>.*?<\/div>/)
// Matches: "<div>hello</div>"

Character Classes

PatternMatches
[abc]Any of a, b, or c
[^abc]Any character except a, b, c
[a-z]Any lowercase letter
[A-Za-z]Any letter
[0-9]Any digit (same as \d)
[a-zA-Z0-9_]Any word character (same as \w)

Groups and Alternation

// Capturing group - extracts matched text
/(\d{4})-(\d{2})-(\d{2})/.exec("2026-03-09")
// Groups: ["2026-03-09", "2026", "03", "09"]

// Named capturing group
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/.exec("2026-03-09")
// groups: { year: "2026", month: "03", day: "09" }

// Non-capturing group (grouping without capturing)
/(?:http|https):\/\//
// Groups for alternation but doesn't capture

// Alternation (OR)
/cat|dog/         // Matches "cat" or "dog"
/(red|blue) car/  // Matches "red car" or "blue car"

Lookaheads and Lookbehinds

These match a position without consuming characters. They're zero-width assertions:

PatternNameMeaning
(?=...)Positive lookaheadFollowed by ...
(?!...)Negative lookaheadNOT followed by ...
(?<=...)Positive lookbehindPreceded by ...
(?<!...)Negative lookbehindNOT preceded by ...
// Match a number followed by "px"
/\d+(?=px)/.exec("font-size: 16px")  // "16"

// Match a word NOT followed by a comma
/\w+(?!,)/

// Match a number preceded by "$"
/(?<=\$)\d+/.exec("Price: $42")  // "42"

// Match "script" NOT preceded by "Java"
/(?<!Java)script/

Flags

FlagNameEffect
gGlobalFind all matches, not just the first
iCase-insensitive/hello/i matches Hello, HELLO
mMultiline^ and $ match line starts/ends
sDotall. matches newline characters too
uUnicodeEnables full Unicode support

Common Real-World Patterns

Email Address (simplified)

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

Note: A fully RFC-compliant email regex is absurdly long. For production, use your language's email validation library.

URL

/https?:\/\/[^\s/$.?#].[^\s]*/i

IPv4 Address

/^(\d{1,3}\.)3\d{1,3}$/

This validates the format but not the range (allows 999.999.999.999). For strict validation, check each octet is 0-255.

Phone Number (US formats)

/^(\+1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/

Matches: (555) 123-4567, 555-123-4567, +1 555.123.4567

Date (YYYY-MM-DD)

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

Hex Color

/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/

Matches: #fff, #FF5733

HTML Tags

/<([a-z][a-z0-9]*)\b[^>]*>(.*?)<\/\1>/gi

Warning: Don't parse HTML with regex for anything serious. Use a proper HTML parser. This is fine for quick search-and-replace tasks.

Password Strength

// At least 8 chars, 1 uppercase, 1 lowercase, 1 digit, 1 special
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/

Whitespace Cleanup

// Trim leading/trailing whitespace
str.replace(/^\s+|\s+$/g, '')

// Collapse multiple spaces to one
str.replace(/\s+/g, ' ')

// Remove blank lines
str.replace(/^\s*\n/gm, '')

JavaScript Regex Methods

MethodReturnsUse Case
str.match(regex)Array of matches or nullFind matches
str.matchAll(regex)Iterator of match objectsFind all with groups
str.replace(regex, str)New stringFind and replace
str.search(regex)Index or -1Find position
str.split(regex)Array of partsSplit by pattern
regex.test(str)BooleanCheck if matches
regex.exec(str)Match object or nullDetailed match info

Tips for Writing Better Regex

  • Start simple, add complexity — Get the basic pattern working first, then handle edge cases.
  • Use named groups(?<year>\d{4}) is clearer than (\d{4}) when you have multiple groups.
  • Avoid catastrophic backtracking — Nested quantifiers like (a+)+ can cause exponential runtime. Use atomic groups or possessive quantifiers when available.
  • Test with edge cases — Empty strings, very long input, Unicode characters, and newlines often break regex patterns.
  • Add comments — Use the x flag (verbose mode) in Python/Ruby to add whitespace and comments to complex patterns.
  • Use a testing tool — Try our Regex Tester to build and debug patterns interactively with highlighted matches.

Regex is a skill that improves with practice. Bookmark this cheat sheet for reference, and use our Regex Tester to experiment with patterns in real-time.

Drop file to load