.* Free Developer Tool · No Signup

Regex Tester & Explainer

Test regular expressions with real-time match highlighting, capture group extraction, and plain English explanations. Runs entirely in your browser — your data never leaves your machine.

✓ 100% client-side ✓ No data sent ✓ Real-time matching ✓ Free forever
/ /
Test String
Match Results
Enter a regex and test string to see matches
Regex Explainer
Enter a regex pattern to see a plain English breakdown
Common Patterns
Substitution Mode
Replace:
Result:
Enter a replacement string to see the result
✓ Valid 0 matches 0 groups Flags: g

The Complete Guide to Regular Expressions

What Are Regular Expressions?

Regular expressions (regex or regexp) are a compact, powerful language for describing text patterns. Originally formalized by mathematician Stephen Kleene in 1956 and first implemented in software by Ken Thompson in 1968 for the QED text editor, regex has since become one of the most ubiquitous tools in computing. Every major programming language, every serious text editor, and every Unix-like operating system supports regular expressions in some form.

At their core, regular expressions let you answer the question: "Does this string match this pattern?" But they go far beyond simple matching. You can extract specific parts of a string (capture groups), replace matched text with something else (substitution), split strings at pattern boundaries, and validate that input conforms to a specific format. The email validation regex, the URL parser, the log file analyzer — all of these are regex at work.

The reason regex has survived for over fifty years while countless other technologies have come and gone is that text pattern matching is a genuinely hard problem, and regex provides a remarkably concise solution. A single regex pattern can replace dozens of lines of procedural string-matching code. The tradeoff is readability: regex has a well-earned reputation for being "write-only" code. This guide, and the interactive tool above, exist to make that tradeoff less painful.

Regex Syntax Basics

Literal characters match themselves. The pattern cat matches the literal string "cat" inside any text. Most characters are literal — letters, digits, spaces. The exceptions are the metacharacters that carry special meaning.

Metacharacters are the building blocks of regex power. There are fourteen of them: . ^ $ * + ? { } [ ] ( ) | \. Each has a specific role. The dot . matches any single character (except newline, unless the s flag is set). The caret ^ and dollar $ anchor patterns to the start and end of a line. The backslash \ escapes a metacharacter to make it literal, so \. matches an actual dot.

Character classes let you match any one character from a set. [abc] matches "a", "b", or "c". [a-z] matches any lowercase letter. [^abc] matches any character except "a", "b", or "c" (the caret inside square brackets means negation). Shorthand classes save keystrokes: \d matches any digit (equivalent to [0-9]), \w matches any word character (letters, digits, and underscore), and \s matches any whitespace character (spaces, tabs, newlines).

Quantifiers control how many times a pattern element repeats. * means zero or more, + means one or more, and ? means zero or one (optional). Curly braces give precise control: {3} means exactly 3, {2,5} means between 2 and 5, and {3,} means 3 or more. All quantifiers are greedy by default (they match as much as possible); append ? to make them lazy (match as little as possible).

Anchors assert a position without consuming characters. ^ matches the start of the string (or line, with the m flag). $ matches the end. \b matches a word boundary — the position between a word character and a non-word character. This is crucial for matching whole words: \bcat\b matches "cat" but not "concatenate".

Groups and capturing use parentheses. (abc) captures the matched text in a group for later reference. (?:abc) is a non-capturing group (for grouping without capturing). Lookaheads (?=abc) and lookbehinds (?<=abc) assert that a pattern exists ahead or behind the current position without including it in the match.

Flags modify how the entire pattern behaves. g (global) finds all matches, not just the first. i (case-insensitive) ignores case distinctions. m (multiline) makes ^ and $ match line boundaries. s (dotAll) makes . match newline characters.

10 Most Useful Regex Patterns

#PatternDescriptionExample Match
1[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}Email addressuser@example.com
2https?://[^\s/$.?#].[^\s]*URLhttps://example.com/page
3\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}US phone number(555) 123-4567
4\b\d{1,3}(\.\d{1,3}){3}\bIPv4 address192.168.1.1
5\d{4}-\d{2}-\d{2}Date (YYYY-MM-DD)2026-04-30
6<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>.*?</\1>HTML tag pair<div class="x">text</div>
7#([0-9a-fA-F]{3}){1,2}\bHex color code#ff5733, #abc
8^\s+|\s+$Leading/trailing whitespace  hello  
9\b(\w+)\s+\1\bDuplicate wordsthe the
10(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^a-zA-Z\d]).{8,}Strong password (8+, upper, lower, digit, special)P@ssw0rd!

Common Regex Mistakes

Forgetting to escape special characters. If you want to match a literal dot, period, or bracket, you must use \., \(, \[, etc. One of the most common bugs is writing 192.168.1.1 to match an IP address — the unescaped dots will match any character, so "192x168y1z1" would also match. Use 192\.168\.1\.1 instead.

Greedy vs. lazy matching. The pattern <.*> applied to <b>bold</b> matches the entire string from the first < to the last >, not just <b>. That is because .* is greedy — it consumes as much as possible. Use <.*?> (lazy) to match the shortest possible string, which gives you <b> and </b> as separate matches.

Catastrophic backtracking. Certain regex patterns can cause the engine to try an exponential number of paths, freezing your program. The classic example is (a+)+$ applied to a string like "aaaaaaaaaaaaaab". The engine tries every possible way to divide the "a"s among the nested quantifiers before finally failing. Avoid nesting quantifiers like (a+)+, (a*)*, or (a|a)+.

Not anchoring patterns. If you want to validate that an entire string is an email, you need ^pattern$. Without anchors, the pattern will match an email substring inside any larger string, giving false positives on input like "definitely not an email user@example.com in here".

Using regex when you should not. Regex cannot parse nested structures like HTML, JSON, or programming languages. They operate on regular languages (in the formal computer science sense), which cannot track arbitrary nesting depth. Use a proper parser for HTML (DOMParser), JSON (JSON.parse), or any context-free grammar.

Regex in Different Languages

JavaScript has first-class regex support with literal syntax /pattern/flags and the RegExp constructor. Key methods: regex.test(str) returns a boolean, str.match(regex) returns matches, str.replace(regex, replacement) performs substitution, and str.matchAll(regex) returns an iterator of all matches with groups.

const re = /(\d{4})-(\d{2})-(\d{2})/g;
const str = 'Dates: 2026-04-30 and 2026-05-01';
for (const m of str.matchAll(re)) {
  console.log(`Year: ${m[1]}, Month: ${m[2]}, Day: ${m[3]}`);
}

Python uses the re module with raw strings to avoid backslash confusion. re.search(pattern, string) finds the first match, re.findall() returns all matches, and re.sub() handles substitution. Always use raw strings (r"pattern") so that \d is not interpreted as an escape sequence.

import re
pattern = r'(\d{4})-(\d{2})-(\d{2})'
matches = re.findall(pattern, 'Dates: 2026-04-30 and 2026-05-01')
for year, month, day in matches:
    print(f'Year: {year}, Month: {month}, Day: {day}')

Go uses the regexp package with RE2 syntax (no backreferences or lookaheads). regexp.MustCompile() compiles a pattern and panics on error. FindAllString(), FindStringSubmatch(), and ReplaceAllString() handle common operations.

re := regexp.MustCompile(`(\d{4})-(\d{2})-(\d{2})`)
matches := re.FindAllStringSubmatch("2026-04-30", -1)
fmt.Println(matches[0][1]) // "2026"

Bash/grep provides regex through command-line tools. grep -E (or egrep) uses extended regex. sed performs regex-based find-and-replace on streams. awk matches patterns in structured text. Use grep -P for Perl-compatible regex (PCRE) with lookaheads and other advanced features.

# Find all email addresses in a file
grep -oE '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}' contacts.txt

# Replace dates from MM/DD/YYYY to YYYY-MM-DD
sed -E 's|([0-9]{2})/([0-9]{2})/([0-9]{4})|\3-\1-\2|g' data.csv

Performance Tips

Avoid catastrophic backtracking. Never nest quantifiers like (a+)+. If you must use alternation inside a repeated group, make sure the alternatives cannot match the same text. Use atomic groups or possessive quantifiers (in engines that support them) to prevent unnecessary backtracking.

Use non-capturing groups. Every () group allocates memory to store the captured text. If you only need grouping for alternation or quantification, use (?:) instead. In performance-critical code with thousands of matches, this makes a measurable difference.

Be specific. Replace .* with more specific patterns whenever possible. [^"]* is dramatically faster than .*? for matching the contents of a quoted string, because it never needs to backtrack. The more specific your character class, the fewer paths the engine explores.

Compile outside loops. In Python (re.compile()), Go (regexp.MustCompile()), and Java (Pattern.compile()), compile your regex once and reuse the compiled object. JavaScript handles this automatically with regex literals, but if you use new RegExp() inside a loop, you are paying the compilation cost on every iteration.

Use word boundaries for precision. Searching for cat will match inside "concatenate", "catalog", and "scattered". Adding \b word boundaries (\bcat\b) restricts the match to the standalone word, which is both more correct and often faster because the engine can skip non-boundary positions entirely.

Frequently Asked Questions

What is regex?+
Regex (short for regular expression) is a pattern matching language used to search, match, and manipulate text. It is supported in virtually every programming language and text editor. A regex pattern like \d{3}-\d{4} matches phone number fragments like "555-1234". Regular expressions can validate input (emails, dates, passwords), extract data from text (log files, HTML), and perform complex find-and-replace operations.
Is this regex tester free?+
Yes, 100% free with no signup, no email required, and no paywall. Everything runs in your browser using JavaScript. Your test strings and patterns are never sent to any server. You can verify this by opening your browser's Network tab — you will see zero outbound requests when using the tool. It even works offline after the first page load.
What regex flavor does this tool use?+
This tool uses JavaScript's built-in RegExp engine (ECMAScript specification). It supports all standard flags: global (g), case-insensitive (i), multiline (m), and dotAll (s). The syntax is compatible with most modern regex engines, though some features like lookbehinds may not work in older browsers. Named capture groups ((?<name>...)) are supported in all modern browsers.
Can regex match nested structures like HTML?+
Generally no. Regular expressions operate on regular languages (in the formal computer science sense), which cannot handle arbitrary nesting. While you can match simple HTML tags with regex, parsing nested or malformed HTML reliably requires a proper parser like DOMParser in JavaScript or BeautifulSoup in Python. The same applies to JSON, XML, and programming language syntax. Use the right tool for the job.
What is the difference between * and + in regex?+
The * quantifier matches zero or more occurrences of the preceding element, while + matches one or more. For example, a* matches an empty string, "a", "aa", "aaa", and so on. But a+ requires at least one "a", so it matches "a", "aa", "aaa" but not an empty string. Use * when the element is optional, and + when at least one occurrence is required.

Built for Developers, by Developers

Check out our other free developer tools, or browse thousands of open engineering roles at top AI and tech companies.

All Developer Tools → Browse 14,600+ AI & Tech Jobs →