Learn regular expressions from scratch — syntax, anchors, character classes, quantifiers, and groups with practical examples.
A regular expression (regex) is a pattern that describes a set of strings. It's used to search, validate, and transform text.
const pattern = /hello/;
pattern.test("hello world"); // true
pattern.test("goodbye"); // false
The simplest regex matches literal text: /cat/ matches the string 'cat' anywhere in the input.
Special characters that need escaping: . * + ? ^ $ { } [ ] | ( ) \
[abc] - matches a, b, or c
[a-z] - any lowercase letter
[0-9] - any digit
[^abc] - NOT a, b, or c
\d - digit [0-9]
\w - word character [a-zA-Z0-9_]
\s - whitespace
. - any character (except newline)
^ - start of string
$ - end of string
\b - word boundary
/^hello/.test('hello world') // true
/world$/.test('hello world') // true
/\bcat\b/.test('cats') // false — not a whole word
* - 0 or more
+ - 1 or more
? - 0 or 1 (optional)
{n} - exactly n times
{n,m} - between n and m times
/(cat|dog)/ // matches 'cat' or 'dog'
const match = '2025-02-22'.match(/(\d{4})-(\d{2})-(\d{2})/);
match[1] // '2025'
match[2] // '02'
i - case-insensitive
g - global (find all matches)
m - multiline (^ and $ match line boundaries)
Practice with the Regex Tester — with real-time match highlighting.