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

SymbolMeansExample
.Any character except newlinea.cmatches "abc", "a1c"
*0 or more of previousab*cmatches "ac", "abc", "abbc"
+1 or more of previousab+cmatches "abc", not "ac"
?0 or 1 of previous (optional)colou?rmatches "color" and "colour"
^Start of string^Helloonly matches if starts with "Hello"
$End of stringworld$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
\dAny digit (0-9)\d+ matches one or more digits
\wWord 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]+/g

Matches 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+/g

Matches # followed by word characters. The g flag finds all matches.

Extract content inside quotes

/"([^"]+)"/g

Captures everything inside double quotes. The capturing group ([^"]+)returns just the content without the quotes.

Trim leading and trailing whitespace

/^s+|s+$/g

Replace 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})/g

Matches 6-digit (#ffffff) and 3-digit (#fff) hex color codes.

Flags you need to know

  • g — global: find all matches, not just the first
  • i — case-insensitive: treat A and a as the same
  • m — multiline: ^ and $ match the start/end of each line, not just the whole string
  • s — 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.