🔍

Regex Tester

Test and debug regular expressions with real-time matching and highlighting

Pattern

Enter pattern
Flags

Test Input

Statistics

Pattern Length:0
Input Length:0
Matches Found:0
Execution Time:0.00ms

What is a Regular Expression?

A regular expression (regex or regexp) is a sequence of characters that defines a search pattern for matching and extracting text. Instead of searching for exact literal text, regex allows you to specify flexible patterns that match classes of strings. For example, the pattern \b\w+@\w+\.\w+\b matches any email address without knowing the specific address beforehand. Regex works by using special characters called metacharacters to define rules: . matches any single character, * means zero or more repetitions, + means one or more, ? means zero or one, and [] defines character classes. Parentheses create capture groups that extract specific parts of the matched text.

Regular expressions are supported in virtually every programming language and text editor. They're used for input validation (email, phone, credit card formats), text extraction from logs or HTML, search-and-replace operations, parsing structured strings, and form field validation. Learning regex unlocks powerful text processing capabilities, though complex patterns can be difficult to read and maintain. Most languages support regex flags that modify matching behavior: g (global—find all matches), i (case-insensitive), m (multiline—^ and $ match line boundaries), s (dotall—. matches newlines), u (Unicode), and y (sticky).

Common Use Cases

  • Input Validation: Validate email addresses, phone numbers, postal codes, credit cards, URLs, and IP addresses using regex patterns. A pattern like ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$ ensures email format without sending test messages.
  • Text Extraction from Logs: Extract error messages, timestamps, IP addresses, or request IDs from application logs. Regex patterns can pull specific data from millions of log lines instantly, enabling automated log analysis and alerting.
  • Search-and-Replace Operations: Find all occurrences of a pattern and replace with different text. For example, replace URLs in markdown files with HTML links using a regex pattern and replacement template ($1 for captured groups).
  • Parsing Structured Strings: Extract components from structured text like CSV data, HTML attributes, configuration files, or custom formats. Capture groups extract different parts: a pattern like (\d+)-(\d+)-(\d+) extracts date components.
  • Form Field Validation: Client-side validation before submitting forms. Real-time validation as users type provides instant feedback (password strength, username format, etc.) without server round-trips.

How to Use This Tool

  1. Enter Pattern: Type your regex pattern in the Pattern field. The pattern syntax follows JavaScript regex rules (ECMA 262 standard). Start with simple patterns like \d+ (one or more digits) before advancing to complex ones.
  2. Set Flags: Select regex flags that modify behavior. g (global) finds all matches, i (case-insensitive) ignores case, m (multiline) treats ^ and $ as line boundaries, s (dotall) makes . match newlines.
  3. Enter Test Input: Paste the text you want to match against. The tool tests your regex in real-time as you type and shows which portions of the text match your pattern.
  4. View Highlighted Matches: Matching text is highlighted with different colors for each match. Hover over matches to see their exact position and length. Capture groups are extracted and displayed separately.
  5. Replace Mode (Optional): Switch to Replace mode to test replacement logic. Enter a replacement string with $1, $2, etc. for capture groups, and see the result of replacing all matches.

Features

Real-Time Matching

Pattern and input validation happen instantly as you type. Invalid regex shows error immediately. Matches count and position update in real-time without needing a submit button.

Match Highlighting

All matches are highlighted in different colors with a legend showing which color represents which match. Click a match to view detailed information including position, length, and capture groups.

Capture Groups

Parentheses in patterns create capture groups that extract parts of matches. Named groups and numbered groups are both supported and displayed with their values for each match found.

Replace Mode

Test replacements using $0 (full match), $1/$2/$3 (capture groups), etc. See the result of replacing all matches instantly, useful for complex transformations and text processing.

Frequently Asked Questions

What is a regular expression and when should I use it?

A regular expression is a pattern for matching text. Use regex when you need flexible pattern matching (not exact matches), validation rules (email, phone, format checking), text extraction from larger documents, or search-and-replace with pattern rules. For simple exact text searches, plain string matching is faster and more readable.

What does the "g" flag do?

The g (global) flag makes the regex find ALL matches instead of stopping at the first match. Without g, .match() returns only the first match. With g, it returns an array of all matches. For replacement, .replace() replaces all matches with g, but only the first without it. Always use g when you need to find or replace multiple occurrences.

How do I match a literal dot or bracket?

In regex, . and [ ] have special meanings (. matches any character, [ ] defines character class). To match them literally, escape with a backslash: \. matches a dot, \[ matches a bracket. Any metacharacter can be escaped with backslash to match literally. This is essential when matching file names or JSON with dots and brackets.

What is a capture group and how do I use it?

Parentheses in a pattern create a capture group that extracts part of the matched text. In the pattern (\d+)-(\d+)-(\d+), capturing the date "2024-03-15" produces three groups: $1=2024, $2=03, $3=15. Use capture groups in replace operations to rearrange parts: "2024-03-15" becomes "03/15/2024" with replacement "$2/$3/$1".