Test all patterns as you read this
Open the free regex tester in a new tab. Paste any pattern from this article and test it against your own strings in real time.
The 10 building blocks
| Symbol | Means | Example |
|---|---|---|
. | Any character except newline | a.c matches "abc", "a1c" |
* | 0 or more of previous | ab*c matches "ac", "abc", "abbc" |
+ | 1 or more of previous | ab+c matches "abc", not "ac" |
? | 0 or 1 of previous (optional) | colou?r matches "color" and "colour" |
^ | Start of string | ^Hello only matches if starts with "Hello" |
$ | End of string | world$ only matches if ends with "world" |
[abc] | Any character in set | [aeiou] matches any vowel |
[^abc] | Any character NOT in set | [^0-9] matches any non-digit |
\d | Any digit (0-9) | \d+ matches one or more digits |
\w | Word character (letter, digit, _) | \w+ matches a word |
The patterns you will actually use
Email validation
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/Validates the structure of an email address. Note: this does not verify the email exists — only that it looks like a valid format. For production, send a confirmation email instead of relying on validation alone.
URL matching
/https?://[^s]+/gMatches http:// and https:// URLs. The ?makes the 's' optional. The [^\s]+ matches everything up to whitespace.
Phone numbers (flexible)
/(+?d{1,3}[s-]?)?(?d{3})?[s.-]?d{3}[s.-]?d{4}/Matches most international and US phone number formats. Phone number formats are famously inconsistent — consider normalizing before validating.
Numbers only
/^d+$/Validates that a string contains only digits. Useful for ZIP codes, IDs, PINs.
Alphanumeric only
/^[a-zA-Z0-9]+$/Strong password (8+ chars, upper, lower, digit, symbol)
/^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*[@$!%*?&])[A-Za-zd@$!%*?&]{8,}$/Uses lookaheads ((?=...)) to require each character class without specifying position.
Find all hashtags in text
/#w+/gMatches # followed by word characters. The g flag finds all matches.
Extract content inside quotes
/"([^"]+)"/gCaptures everything inside double quotes. The capturing group ([^"]+)returns just the content without the quotes.
Trim leading and trailing whitespace
/^s+|s+$/gReplace with an empty string to trim. Note: in JavaScript, str.trim()does the same thing more readably — use the regex when you need more control.
Match hex color codes
/#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/gMatches 6-digit (#ffffff) and 3-digit (#fff) hex color codes.
Flags you need to know
g— global: find all matches, not just the firsti— case-insensitive: treat A and a as the samem— multiline: ^ and $ match the start/end of each line, not just the whole strings— dotAll: make . match newlines too
Summary
The patterns above cover the majority of real-world regex use. Test them with the free regex tester — paste the pattern, paste your test string, see matches highlighted in real time. For complex patterns, read them from left to right and break them into smaller pieces.