Regular expressions describe patterns of text for searching, validating and replacing. This is the syntax shared by JavaScript, Python, Java, PHP, VS Code and most other tools, each piece shown matching a real example.
Practise by typing8 short lessons with spaced review. Write each pattern; any regex that works counts.
Characters and classes
9 patterns
Pattern
What it does
Example
.
Match any single character except a line breakThe dot. With the s flag it matches line breaks too.
a1-
\d
Match one digit, 0 to 9\d is short for [0-9] (in JavaScript; Unicode digits elsewhere with some flags).Also: [0-9]
Room 42B
\D
Match one character that is not a digitCapital letters negate the shorthand classes: \D, \W, \S.Also: [^0-9], [^\d]
Room 42B
\w
Match one word character: a letter, digit or underscore\w is [A-Za-z0-9_]. Accented letters are not included unless you use Unicode classes.Also: [A-Za-z0-9_], [a-zA-Z0-9_]
id_7 = x!
\s
Match one whitespace character (space, tab or line break)Covers space, tab, newline, carriage return and other Unicode spaces.
a b\tc
[aeiou]
Match one vowel: a, e, i, o or uSquare brackets match exactly one of the characters inside, in any order.
regular
[^aeiou]
Match one character that is not a vowelA ^ right after [ negates the set.
regular
[a-z]
Match one lowercase letter from a to zA hyphen between two characters makes a range. [A-Za-z] covers both cases.
Hello
[0-9a-fA-F]
Match one hexadecimal digit (0–9, a–f, A–F)Several ranges can share one set.Also: [0-9A-Fa-f], [a-fA-F0-9]
#1f9
Quantifiers
7 patterns
Pattern
What it does
Example
a*
Match the letter a zero or more times* means zero or more of the thing before it.
baaad
\d+
Match one or more digits+ means one or more.Also: [0-9]+
Order 66 in 2026
colou?r
Match colour or color (the u is optional)? makes the thing before it optional: zero or one.
color or colour
\d{3}
Match exactly 3 digits{n} repeats exactly n times.Also: [0-9]{3}
Call 555 0199
[a-z]{2,4}
Match 2 to 4 letters a to z{min,max} repeats between min and max times.
a bb cccccc
.{8,}
Match 8 or more of any character{n,} means n or more, with no upper limit.
hunter2 vs correcthorse
<.*?>
Match a < then as few characters as possible up to the first >Adding ? after a quantifier makes it lazy: it stops at the first chance instead of the last.Also: <.+?>, <[^>]*>
<b>bold</b>
Anchors and boundaries
5 patterns
Pattern
What it does
Example
^Hello
Match Hello only at the start of the text^ matches the start of the text (or of each line with the m flag).
Hello, and Hello again
\.js$
Match .js only at the end of the text$ matches the end of the text (or of each line with the m flag). The dot is escaped so it means a real dot.
app.js.map and app.js
^\d+$
Match text that is digits only, from start to end^ and $ together force the whole text to match, the usual way to validate input.Also: ^[0-9]+$
2026
\bcat\b
Match cat as a whole word, not inside category\b is the edge between a word character and a non-word character.
cat, category, bobcat
\Bcat\B
Match cat only when it is inside a longer word\B matches where \b does not.
cat, concatenate
Groups and alternation
5 patterns
Pattern
What it does
Example
cat|dog
Match cat or dog| means or. It splits the whole pattern unless you put it in a group.Also: dog|cat, (cat|dog)
cat, dog, cow
(ha)+
Match ha repeated one or more times (ha, haha, hahaha)Parentheses group several characters so a quantifier applies to all of them.Also: (?:ha)+
hahaha!
(?:https://)?example\.com
Match example.com with an optional https:// in front, grouping https:// without capturing it(?:…) groups without saving the match, which is faster and keeps capture numbers simple.
https://example.com
(?<year>\d{4})
Capture four digits in a group named yearRead it back as match.groups.year in JavaScript or (?P<year>…) style names in Python.Also: (?<year>[0-9]{4})
Released 2026
\b(\w+) \1\b
Match a doubled word such as the the\1 matches the same text the first group matched.Also: (\w+) \1, \b(\w+)\s\1\b
it was the the end
Lookarounds
4 patterns
Pattern
What it does
Example
\d+(?=px)
Match a number only when it is followed by px (without matching px)(?=…) checks what comes next without including it in the match.Also: [0-9]+(?=px)
12px 3em 40px
foo(?!bar)
Match foo only when it is not followed by bar(?!…) succeeds when the text ahead does not match.
foobar foobaz
(?<=\$)\d+
Match a number only when it comes after a $ sign (without the $)(?<=…) checks what comes before. Supported in modern JavaScript, Python, Java and .NET.Also: (?<=\$)[0-9]+
Pay $30, not 30
(?<![-\d])\d+
Match a number not preceded by a minus sign or another digit(?<!…) succeeds when the text behind does not match.Also: (?<![\d-])\d+, (?<!-|\d)\d+
-5 and 8
Flags
4 patterns
Pattern
What it does
Example
/hello/i
Match hello in any letter case (write it as /pattern/flags)i ignores case. In Python use re.IGNORECASE.
Hello HELLO
/cat/g
Find every cat, not just the first (write it as /pattern/flags)g finds all matches, used by replaceAll, matchAll and replace with every match.
cat and cat
/^#/m
Make ^ match at the start of every line: match # at line starts (write it as /pattern/flags)m makes ^ and $ work per line instead of for the whole text.Also: /^#/gm, /^#/mg
# one\ntwo\n# three
/a.*b/s
Let the dot match line breaks too: match a then anything then b (write it as /pattern/flags)s (dotAll) makes . match newlines.Also: /a.+b/s
a\nb
Escaping
4 patterns
Pattern
What it does
Example
\.
Match a real full stop (period)A backslash turns a special character into a plain one. Inside [ ] most characters are plain already.Also: [.]
v1.2
\(1\+1\)
Match the text (1+1) exactlyEscape ( ) + and the other specials: . * + ? ^ $ { } [ ] | \ /
x = (1+1)
\\
Match one backslash characterA backslash escapes itself. In a JavaScript or Python string you may need to double it again.
C:\Users
\r?\n
Match a line break, either Windows (\r\n) or Unix (\n)\n is a newline, \r a carriage return and \t a tab.
one\r\ntwo\nthree
Common patterns
8 patterns
Pattern
What it does
Example
-?\d+
Match a whole number with an optional minus sign-? for the sign, \d+ for the digits.Also: -?[0-9]+
-12 and 40
\d+(\.\d+)?
Match a number with an optional decimal part, such as 3 or 3.14The decimal point and the digits after it are grouped and made optional together.Also: \d+(?:\.\d+)?, [0-9]+(\.[0-9]+)?
pi is 3.14, not 3.
#[0-9a-fA-F]{6}
Match a hex colour: # then exactly 6 hex digitsUse {3}|{6} or [0-9a-f]{3}(?:[0-9a-f]{3})? to allow the short #abc form too.Also: #[0-9A-Fa-f]{6}, #[a-fA-F0-9]{6}
color: #1A2b3C;
\d{4}-\d{2}-\d{2}
Match a date written YYYY-MM-DD, such as 2026-11-21This checks the shape only; it accepts 2026-13-45.Also: [0-9]{4}-[0-9]{2}-[0-9]{2}
Due 2026-11-21.
\S+@\S+\.\S+
Match a simple email: non-space characters, @, non-space characters, a dot, non-space charactersGood enough to catch typing mistakes. Real address rules are far looser; send a confirmation email instead of a stricter regex.Also: [^\s@]+@[^\s@]+\.[^\s@]+
Mail ada@example.com today
^\s+|\s+$
Match spaces at the start or the end of the text, to trim themUse it with the g flag in replace(…, ""). Most languages also have a trim() function.Also: \s+$|^\s+, ^\s*|\s*$
padded
/^\s*$/m
Match an empty line or one holding only spaces (with the m flag)With m, ^ and $ match at each line, so the pattern finds every blank line.Also: /^\s*$/gm, /^\s*$/mg
a\n\nb
\b(red|green|blue)\b
Match any one of the whole words red, green or blueGroup the alternatives and put the word boundaries outside the group.Also: \b(?:red|green|blue)\b
red, greenish, blue
Test a regex
Syntax as in JavaScript (ECMAScript 2025), checked against the MDN regex reference; Python, Java, PHP, .NET and most editors use the same pieces, with small differences in lookbehind and named groups. To learn them, open the Regex practice: any pattern that does the job is accepted.