Regex for People Who Keep Putting It Off
Regular expressions made approachable. The patterns you'll actually use, explained in plain English, with real examples you can run right now.
Regex has a reputation for being intimidating. That reputation is somewhat deserved — a long regex with nested groups and lookaheads genuinely is hard to read. But 80% of real-world regex use comes down to about ten concepts, and those ten concepts take an hour to learn.
Start Here: What Regex Actually Does
A regular expression is a pattern that matches text. /hello/ matches any string containing 'hello'. /^hello$/ matches only the exact string 'hello'. The pattern defines a rule; the engine checks whether a string follows that rule.
The 10 Patterns That Cover 80% of Real Use
- . (dot): Matches any single character except newline. /h.t/ matches 'hot', 'hat', 'hit', 'h!t'.
- * (asterisk): Matches 0 or more of the previous character. /he*llo/ matches 'hllo', 'hello', 'heello'.
- + (plus): Matches 1 or more. /he+llo/ won't match 'hllo' but will match 'hello'.
- ? (question mark): Makes the previous character optional. /colou?r/ matches both 'color' and 'colour'.
- [abc] (character class): Matches any of the listed characters. /[aeiou]/ matches any vowel.
- [^abc] (negated class): Matches anything NOT in the brackets. /[^0-9]/ matches any non-digit.
- \d: Shorthand for [0-9] (digit). \w for word characters [a-zA-Z0-9_]. \s for whitespace.
- ^ and $: Start and end of string. Essential for exact matching.
- | (pipe): OR operator. /cat|dog/ matches 'cat' or 'dog'.
- (group): Captures a group for reference or quantification. /(ha)+/ matches 'ha', 'haha', 'hahaha'.
Three Patterns You Can Use Today
Email validation: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ — This isn't perfect (no regex is for email) but catches 99% of obvious mistakes.
Phone number (US): /^\+?1?\s?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/ — Handles (555) 123-4567, 555-123-4567, 5551234567.
URL: /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b[-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*/ — Verbose but reliable for basic URL detection.
The Mistake Everyone Makes First
Greedy matching. By default, quantifiers (* and +) are greedy — they match as much as possible. If you're trying to extract content between HTML tags with /<b>.*<\/b>/, on the string '<b>hello</b> and <b>world</b>', it'll match the entire thing from the first opening tag to the last closing tag. Fix: use .*? (lazy) instead of .* to stop at the first possible match.
How to Read an Existing Regex
Break it into segments. Read left to right. Name each segment in English. A good test: if you can't explain what a regex does in one sentence, it's probably doing too much. Split it into multiple simpler patterns or add a comment explaining each section. Regex can be commented in some engines using the verbose mode flag (x in Python and Ruby).
Frequently Asked Questions
What's the fastest way to learn regex?+
Is regex the same in JavaScript, Python, and other languages?+
When should I NOT use regex?+
What does ^ and $ mean in regex?+
🔧 Free Tools Used in This Guide
FreeToolKit Team
FreeToolKit Team
We build free browser-based tools and write practical guides that skip the fluff.
Tags: