A regular expression, or regex, is a pattern that describes a set of strings — think of it as search with rules instead of fixed text. Regex powers validation on forms, log analysis, search-and-replace in editors, and the pattern matching inside countless tools and frameworks. It looks like gibberish at first, but it is built from a surprisingly small set of ideas, and once you see those, the cryptic lines start to read like instructions. The same pattern syntax appears across languages, so the skills transfer directly from JavaScript to Python to your text editor.
The Building Blocks of Regex
Every regex combines three kinds of elements. Literal characters match themselves, so the letter a matches the letter a. Metacharacters carry special meaning, like the dot that matches any single character. Quantifiers say how often something may repeat. Understanding these three roles explains most of what you see in a pattern. A common beginner trap is reaching for a huge pattern when a small one would do; the simplest pattern that works is usually the best one, because complex regex is easy to break with edge cases.
\d any digit (0-9)
\w any word character: letter, digit, underscore
[abc] any one of a, b, or c
^ start of the string
$ end of the string
* zero or more
+ one or more
? zero or one
Test a pattern instantly
Paste a regex and sample text to see every match highlighted live.
Real Patterns You Can Use Today
- Email address:
[\w.+-]+@[\w-]+\.[\w.-]+finds most addresses inside a block of text. - US phone number:
\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}matches 555-123-4567. - Date in ISO form:
\d{4}-\d{2}-\d{2}matches 2026-08-14. - Whole words only:
\bcat\bmatches "cat" but not "catalog".
Learn by Testing
The fastest way to learn regex is to test it. Copy a pattern you just read, run it against real text, and look at exactly what matches — and, just as importantly, what does not. A regex matcher that highlights matches live makes every change visible instantly, turning pattern-building from trial and error into deliberate practice. Remember also that matching is case-sensitive by default, so decide whether your pattern needs a case-insensitive flag before you test. Start with simple patterns, add one building block at a time, and within a short session the syntax stops looking like a secret code and starts looking like a tool you actually control.