Skip to content

Regex Cheat Sheet: Syntax, Flags and Ready-to-Use Patterns

A regular expression (regex) is a short language for finding, validating or replacing patterns in text. Below is the syntax you'll use most, and ready-made patterns that have all been tested. To try one on your own text, the Regex Tester highlights matches, lists groups in a table and previews replacements.

Do the calculation nowRegex Tester

Core syntax

Most regular expressions are built from these pieces:

TokenMeaning
.Any character except a line break
\d \w \sDigit, word character (letter, digit, underscore), whitespace
\D \W \SNot a digit, not a word character, not whitespace
[abc] [^abc] [a-z]One character in the set, not in the set, in the range
^ $Start and end of the text (of each line with the m flag)
\bWord boundary
* + ?Zero or more, one or more, zero or one
{3} {2,5} {2,}Exactly 3, between 2 and 5, at least 2
(…) (?:…) (?<name>…)Capturing group, non-capturing group, named group
a|ba or b
(?=…) (?!…)Lookahead: must follow, must not follow
\. \( \\The special character itself (escaped)

Flags

Flags change how the pattern is applied:

FlagMeaning
gFind all matches, not just the first
iIgnore case
m^ and $ match at the start/end of every line
sLet . match line breaks too
uUnicode: letter classes like \p{L} and emoji work correctly

Validation patterns

These check a whole field, so they start with ^ and end with $. To search inside longer text instead, drop the ^ and $ and use the g flag.

Email (practical check)
^[^\s@]+@[^\s@]+\.[^\s@]+$

URL (http / https)
^https?:\/\/[^\s/$.?#][^\s]*$

Date, YYYY-MM-DD
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

Time, 24-hour HH:MM
^([01]\d|2[0-3]):[0-5]\d$

IPv4 address
^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$

Hex colour (#fff or #1a2b3c)
^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$

URL slug (lowercase-with-hyphens)
^[a-z0-9]+(?:-[a-z0-9]+)*$

Password: 8+ chars, lower, upper and a digit
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$

Turkey-specific patterns

Removing spaces before checking phone numbers and IBANs keeps the patterns simple. The IBAN pattern checks the format only; a regex cannot verify the check digits.

Turkish mobile number (spaces removed): 05321234567, +905321234567
^(\+90|0)?5\d{9}$

Turkish IBAN (26 characters, no spaces)
^TR\d{24}$

Turkish postal code
^\d{5}$

A word with Turkish letters (needs the u flag)
^\p{L}+$

Find-and-replace recipes

In the replacement, $1, $2 … insert captured groups and $<name> inserts a named group. You can paste these straight into the Regex Tester's replace box.

Collapse repeated spaces
  find: \s{2,}    replace: " " (one space)

Remove a doubled word ("the the")
  find: \b(\w+)\s+\1\b    flags: gi    replace: $1

2026-11-21 → 21/11/2026
  find: (\d{4})-(\d{2})-(\d{2})    replace: $3/$2/$1

Same with named groups
  find: (?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})    replace: $<d>/$<m>/$<y>

Non-English letters

In JavaScript, \w and \b only know the English alphabet: in "café" \w+ stops at the é. To match letters in any language, use \p{L} with the u flag; \p{L}+ matches "café", "çiçek" and "straße" as whole words.

Common mistakes

If a pattern doesn't behave, check these first:

  • Unescaped dots: in example.com the dot matches any character; write \. for a literal dot.
  • Greedy matching: <.+> grabs everything from the first < to the last > on a line. Use <.+?> for the shortest match.
  • Missing anchors: \d{5} also matches inside "123456"; use ^\d{5}$ for exactly five digits.
  • Trying to validate email perfectly: a fully standards-compliant pattern is enormous and still can't tell you the address exists. A simple check is enough; a confirmation email does the real validation.
  • Parsing HTML with regex: nested tags need an HTML parser.

Frequently asked questions

Do the calculation nowRegex Tester