What Is a Regular Expression?
A regular expression (regex) is a sequence of characters that defines a search pattern. The pattern can be as simple as a literal string or as complex as a nested combination of quantifiers, character classes, anchors, and groups. Regex engines compare the pattern against input text and report which portions match.
Regular expressions are built into virtually every programming language and are available in most text editors and command-line tools. Developers rely on a regex tester to validate email addresses, phone numbers, and URL formats in form validation. System administrators use regular expression online tools to filter log lines and extract fields from structured output. Editors like VS Code and Vim treat regex as a first-class search primitive, letting you rename identifiers or reformat dates across thousands of files in seconds.
Three flags control how a match is performed. The global flag (g) finds every occurrence in the input rather than stopping at the first. The case-insensitive flag (i) makes regex match uppercase and lowercase letters interchangeably. The multiline flag (m) redefines the anchor characters ^ and $ so they match at line boundaries instead of only at the very start and end of the string. Understanding when to combine these flags separates brittle patterns from reliable ones.
Regex Flags and Syntax Guide
Open the regex tester and type your pattern into the pattern field. Select any flags you need — g to find all matches, i to ignore case, m to match across lines. Paste representative sample text into the test input below. The tool highlights every match in real time as you type, so you can iterate on the pattern without running any code.
When your pattern includes capture groups, the results panel lists each group's matched value and its character position in the source text. Switch to replace mode to preview substitutions before applying them. Because all processing happens client-side, no data ever leaves your browser.
How to Test a Regular Expression Online
- 1Enter your pattern. Type your regex in the pattern field. Set flags (g, i, m) as needed using the flag toggles beside the input.
- 2Provide test strings. Paste sample text into the test input. Matches are highlighted in real time as you adjust the pattern or flags.
- 3Review matches. The tool shows all matches, capture groups, and their character positions. Switch to replace mode to preview substitutions instantly.
▶Code Examples
▶JavaScript / TypeScript
// Test a pattern with the RegExp constructor
const pattern = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi;
const input = "Contact us at hello@example.com or support@tools.ytlm.space";
// Find all matches
const matches = input.match(pattern);
console.log(matches);
// ["hello@example.com", "support@tools.ytlm.space"]
// Named capture groups
const dateRe = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const { groups } = "2026-04-13".match(dateRe);
console.log(groups); // { year: "2026", month: "04", day: "13" }
// Replace with back-references
const result = "2026-04-13".replace(dateRe, "$<day>/$<month>/$<year>");
console.log(result); // "13/04/2026"▶Python
import re
# Compile a pattern for reuse
pattern = re.compile(r"\b\d{1,3}(?:\.\d{1,3}){3}\b")
text = "Server 192.168.1.1 forwarded to 10.0.0.254"
# Find all matches
matches = pattern.findall(text)
print(matches) # ['192.168.1.1', '10.0.0.254']
# Named capture groups
date_re = re.compile(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})")
m = date_re.search("Release date: 2026-04-13")
if m:
print(m.group("year"), m.group("month"), m.group("day"))
# 2026 04 13
# Substitution
result = date_re.sub(r"\g<day>/\g<month>/\g<year>", "2026-04-13")
print(result) # 13/04/2026▶Go
package main
import (
"fmt"
"regexp"
)
func main() {
// Compile the pattern
pattern := regexp.MustCompile(`\b[\w.+-]+@[\w-]+\.[a-z]{2,}\b`)
input := "Send mail to alice@example.com and bob@go.dev"
// Find all matches
matches := pattern.FindAllString(input, -1)
fmt.Println(matches)
// [alice@example.com bob@go.dev]
// Named capture groups
dateRe := regexp.MustCompile(`(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})`)
match := dateRe.FindStringSubmatch("2026-04-13")
names := dateRe.SubexpNames()
for i, name := range names {
if name != "" && i < len(match) {
fmt.Printf("%s: %s\n", name, match[i])
}
}
// year: 2026 month: 04 day: 13
}▶Bash
# grep -E uses extended regex; -o prints only matching text
echo "Contact: hello@example.com" | grep -Eo '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,}'
# hello@example.com
# Case-insensitive match across a log file
grep -iE 'error|warn' /var/log/app.log
# Extract capture group with sed
echo "2026-04-13" | sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3\/\2\/\1/'
# 13/04/2026
# Count lines matching a pattern
grep -cE '^[[:space:]]*$' file.txt # count blank lines▶Frequently Asked Questions
▶What is a regular expression (regex)?
A regular expression is a pattern of characters that defines a search rule for matching text. It is used in virtually every programming language and text editor.
▶How do I test a regex online?
Type your regex pattern in the pattern field, enter your test text below, and matches are highlighted in real time. You can toggle flags and use replace mode.
▶What do regex flags g, i, and m mean?
g (global) finds all matches. i (case-insensitive) ignores letter casing. m (multiline) makes ^ and $ match the start and end of each line.
▶Can I use capture groups and back-references?
Yes. Use parentheses () to create capture groups. In the replace field, reference them with $1, $2, etc. Named groups (?<name>...) are also supported.