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:

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.

Pattern: hello Text: say hello world Match: ^^^^^

Most characters match themselves literally. The special characters that have meaning in regex are: . * + ? ^ $ { } [ ] ( ) | \

Character Classes

Character classes match one character from a set:

PatternMatchesExample
[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
\dAny digit (same as [0-9])\d{4} matches "2026"
\wWord char [a-zA-Z0-9_]\w+ matches "hello_world"
\sWhitespace (space, tab, newline)\s+ matches spaces
.Any character (except newline)a.c matches "abc", "a1c"

Quantifiers: How Many?

PatternMeaningExample
*0 or moreab*c matches "ac", "abc", "abbc"
+1 or moreab+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
Test your regex patterns live

Type a pattern and see matches highlighted in real time.

Open Regex Tester

Anchors: Where to Match

PatternMeaningExample
^Start of string/line^Hello matches "Hello world" but not "Say Hello"
$End of string/lineworld$ matches "hello world" but not "world cup"
\bWord 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:

// Extract date parts Pattern: (\d{4})-(\d{2})-(\d{2}) Text: 2026-03-22 Group 1: 2026 Group 2: 03 Group 3: 22

The pipe | means "or":

Pattern: (cat|dog|bird) Matches: "cat", "dog", or "bird"

Practical Examples

Validate an Email Address

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Matches: user@example.com, name+tag@sub.domain.org

Match a URL

https?://[^\s/$.?#].[^\s]*

Matches most HTTP and HTTPS URLs in text.

Validate a Strong Password

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

Requires: 8+ characters, uppercase, lowercase, digit, and special character.

Extract IP Addresses

\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b

Matches: 192.168.1.1, 10.0.0.1

Find HTML Tags

<([a-z]+)[^>]*>(.*?)</\1>

Matches paired HTML tags like <p>text</p>. The \1 is a backreference matching the same tag name.

Using Regex in Code

JavaScript

// Test if a string matches const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; emailRegex.test('user@example.com'); // true // Find all matches const text = 'Call 555-1234 or 555-5678'; const matches = text.match(/\d{3}-\d{4}/g); // ["555-1234", "555-5678"] // Replace with regex '2026-03-22'.replace(/(\d{4})-(\d{2})-(\d{2})/, '$2/$3/$1'); // "03/22/2026"

Python

import re # Test if a string matches if re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', email): print('Valid email') # Find all matches phones = re.findall(r'\d{3}-\d{4}', 'Call 555-1234 or 555-5678') # ['555-1234', '555-5678'] # Replace with regex re.sub(r'(\d{4})-(\d{2})-(\d{2})', r'\2/\3/\1', '2026-03-22') # '03/22/2026'

Common Mistakes

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: