How to Use Regex (Regular Expressions)
March 22, 2026 · 8 min read
Regular expressions (regex) are one of the most powerful tools in a developer's arsenal — and one of the most intimidating. A pattern like ^(?=.*[A-Z])(?=.*\d).{8,}$ looks like noise, but once you understand the building blocks, regex becomes intuitive.
This guide takes you from zero to confident with practical examples you can test in our free regex tester.
What Is Regex?
A regular expression is a pattern that describes a set of strings. You use them to:
- Validate input (email, phone number, password strength)
- Search for patterns in text (find all URLs, extract dates)
- Replace text (reformat dates, strip HTML tags)
- Split strings on complex delimiters
Every major programming language supports regex — JavaScript, Python, Java, Go, PHP, Ruby, C#, and more.
The Basics: Literal Characters
The simplest regex is just a literal string. The pattern hello matches the text "hello" anywhere in a string.
Most characters match themselves literally. The special characters that have meaning in regex are: . * + ? ^ $ { } [ ] ( ) | \
Character Classes
Character classes match one character from a set:
| Pattern | Matches | Example |
|---|---|---|
| [abc] | a, b, or c | [aeiou] matches any vowel |
| [a-z] | Any lowercase letter | [a-zA-Z] matches any letter |
| [0-9] | Any digit | [0-9]{3} matches "123" |
| [^abc] | NOT a, b, or c | [^0-9] matches non-digits |
| \d | Any digit (same as [0-9]) | \d{4} matches "2026" |
| \w | Word char [a-zA-Z0-9_] | \w+ matches "hello_world" |
| \s | Whitespace (space, tab, newline) | \s+ matches spaces |
| . | Any character (except newline) | a.c matches "abc", "a1c" |
Quantifiers: How Many?
| Pattern | Meaning | Example |
|---|---|---|
| * | 0 or more | ab*c matches "ac", "abc", "abbc" |
| + | 1 or more | ab+c matches "abc", "abbc" (not "ac") |
| ? | 0 or 1 (optional) | colou?r matches "color" and "colour" |
| {n} | Exactly n | \d{4} matches exactly 4 digits |
| {n,m} | Between n and m | \d{2,4} matches 2-4 digits |
| {n,} | n or more | \w{3,} matches 3+ word chars |
Type a pattern and see matches highlighted in real time.
Open Regex TesterAnchors: Where to Match
| Pattern | Meaning | Example |
|---|---|---|
| ^ | Start of string/line | ^Hello matches "Hello world" but not "Say Hello" |
| $ | End of string/line | world$ matches "hello world" but not "world cup" |
| \b | Word boundary | \bcat\b matches "cat" but not "cats" or "concatenate" |
Groups and Alternation
Parentheses create capturing groups — they group patterns and capture the matched text:
The pipe | means "or":
Practical Examples
Validate an Email Address
Matches: user@example.com, name+tag@sub.domain.org
Match a URL
Matches most HTTP and HTTPS URLs in text.
Validate a Strong Password
Requires: 8+ characters, uppercase, lowercase, digit, and special character.
Extract IP Addresses
Matches: 192.168.1.1, 10.0.0.1
Find HTML Tags
Matches paired HTML tags like <p>text</p>. The \1 is a backreference matching the same tag name.
Using Regex in Code
JavaScript
Python
Common Mistakes
- Forgetting to escape dots — . matches ANY character. To match a literal dot, use \.
- Greedy vs. lazy — .* is greedy (matches as much as possible). Use .*? for lazy (matches as little as possible).
- Not anchoring — Without ^ and $, a pattern matches anywhere in the string. \d{3} matches "123" inside "abc123def".
- Catastrophic backtracking — Nested quantifiers like (a+)+ can cause exponential matching time. Keep patterns simple.
Try It Now
The best way to learn regex is to practice. Use the free regex tester on UtilShed — type a pattern and see matches highlighted in real time.
Related tools you might find useful:
- Regex Tester — test and debug patterns with live highlighting
- Text Case Converter — convert between camelCase, snake_case, etc.
- Slug Generator — create URL-friendly slugs from text
- Remove Duplicate Lines — clean up text data