·8 min read·Blog

How to Use Regex: A Practical Guide for the Patterns You Actually Need

Regex looks like noise until you understand 10 core concepts. Once you do, you can write patterns for almost every real-world use case. Here's the practical guide — no theory, just patterns that work.

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.c matches "abc", "a1c"
*0 or more of previousab*c matches "ac", "abc", "abbc"
+1 or more of previousab+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 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.

Browse by category

Not sure which tool you need? Start with a category.

Everything you can do — for free

No software to buy. No account to create. Just open a tool and get it done.

Work with images

Compress photos before sending them by email, resize pictures for social media, remove backgrounds, or pick the perfect color for a design project — all without installing any app.

Edit and format text

Count words and characters in an essay, compare two documents side by side, convert text to different formats, or generate placeholder text for a presentation.

Stay safe online

Create a strong unique password in one click, check how secure a password is, encode or decode data, and generate secure tokens — your data never leaves your device.

Calculate anything

BMI, loan repayments, unit conversions, date differences, and dozens of other everyday calculations — no spreadsheet or formula knowledge required.

The Free AI Tools is a free collection of 221+ online tools that work directly in your web browser — no download, no installation, no account required. Whether you need to compress an image for email, count words in an essay, generate a strong password, create a QR code for your business, or format JSON for development — you will find a simple, free tool here.

Every tool is privacy-first: your files, text, and data never leave your device. Tools cover image editing, text processing, developer utilities, security & encoding, SEO & web, design & CSS, and more.

☕ Support Us