Published: May 18, 2026
Regular expressions (regex) are patterns used to match character combinations in strings. They may look intimidating at first, but once you learn the basics, you'll wonder how you ever lived without them.
A regular expression is a sequence of characters that defines a search pattern. You can use regex to validate input (like email addresses), find and replace text, extract data from strings, and much more.
Let's start with the fundamentals. Most characters match themselves literally — the pattern cat matches the string "cat" anywhere in your text. Here are the essential building blocks:
hello matches "hello"c.t matches "cat", "cot", "cut"[aeiou] matches any vowel, [0-9] matches any digit[^0-9] matches anything that is NOT a digit\d = digit, \w = word character, \s = whitespaceQuantifiers specify how many times a pattern should appear:
* — zero or more times (e.g., ab*c matches "ac", "abc", "abbc")+ — one or more times (ab+c matches "abc" but not "ac")? — zero or one time (colou?r matches both "color" and "colour"){n} — exactly n times (\d{5} matches a 5-digit ZIP code){n,m} — between n and m timesAnchors don't match characters — they match positions:
^ — start of string (^Hello matches "Hello" only at the beginning)$ — end of string (world$ matches "world" at the end)\b — word boundary (\bcat\b matches "cat" but not "catalog")Parentheses () create groups, and the pipe | means "or":
(apple|banana) matches either "apple" or "banana". Groups also capture matched text for later use: (\w+)@(\w+)\.(\w+) captures the parts of an email address.
^[\w.-]+@[\w.-]+\.\w+$^\d{3}[-.]?\d{3}[-.]?\d{4}$^https?://[\w./-]+$^#[0-9a-fA-F]{6}$The best way to learn regex is practice. Use our free online regex tester to experiment with patterns in real-time. Paste sample text, write a pattern, and see matches highlighted instantly.
Remember: regex is a skill that compounds. Start with simple patterns, test frequently, and gradually tackle more complex expressions. Before long, you'll be writing regex patterns without a second thought!