Regex Tester & Explainer

Test and debug regular expressions with live match highlighting, flag testing, and detailed explanations. Perfect for learning regex, debugging patterns, and validating expressions.

100% Client-Side Processing

Your regex patterns and test text are processed entirely in your browser. No data is sent to any server.

Flags

What is Regex?

Regular expressions (regex) are powerful patterns used to match and manipulate text. They provide a concise way to search, extract, validate, and replace text based on complex rules.

Regex is used in programming languages, text editors, command-line tools, and many applications to:

  • Validate user input (emails, phone numbers, passwords)
  • Search and replace text with patterns
  • Extract specific data from strings
  • Parse and process text files
  • Filter and validate data

While regex can seem complex at first, understanding the basics opens up powerful text processing capabilities. This tool helps you learn and test regex patterns interactively.

Regex Flags

Flags modify how a regular expression behaves. You can combine multiple flags to get the desired behavior.

g — Global

Finds all matches in the string, not just the first one. Without this flag, the regex stops after finding the first match.

Example:

Pattern: /\w+/g
Text: "hello world"
Matches: ["hello", "world"]

Without /g: only ["hello"]

i — Case-Insensitive

Makes the pattern match regardless of case. Uppercase and lowercase letters are treated as equivalent.

Example:

Pattern: /hello/i
Matches: "hello", "Hello", "HELLO", "HeLlO"

m — Multiline

Makes ^ and $ match the start and end of each line, not just the start and end of the entire string.

Example:

Pattern: /^Hello/m
Text: "Hello\nWorld\nHello"
Matches: both "Hello" lines

Without /m: only first "Hello"

s — DotAll

Makes the . (dot) match newline characters as well. Normally, . matches any character except newlines.

Example:

Pattern: /a.b/s
Text: "a\nb"
Matches: "a\nb"

Without /s: no match (dot doesn't match newline)

u — Unicode

Enables full Unicode matching. Treats the pattern as a sequence of Unicode code points, allowing proper handling of emojis, accented characters, and other Unicode symbols.

Example:

Pattern: /[👋🌍]/u
Text: "Hello 👋 World 🌍"
Matches: ["👋", "🌍"]

y — Sticky

Matches only at the position indicated by the lastIndex property. The match must start at this position.

Example:

Pattern: /\w+/y
Text: "hello world"
lastIndex = 6
Matches: "world" (only if lastIndex is at position 6)

Common Regex Patterns

Email Address

\b\w+@\w+\.\w+\b

Basic email pattern (simplified)

Phone Number (US)

\(?\d3\)?[-.\s]?\d3[-.\s]?\d4

Matches formats like (555) 123-4567, 555.987.6543, 555-111-2222

URL

https?://[^\s]+

Matches http:// or https:// URLs

Digits Only

^\d+$

Matches strings containing only digits from start to end

Word Boundaries

\b\w+\b

Matches complete words (not parts of words)

Whitespace

\s+

Matches one or more whitespace characters

Use Cases

  • Form Validation: Validate email addresses, phone numbers, passwords, and other user inputs
  • Data Extraction: Extract specific information from text, logs, or data files
  • Search and Replace: Find and replace text patterns in code, documents, or databases
  • Text Processing: Parse and process structured text data
  • Log Analysis: Filter and extract information from log files
  • Code Refactoring: Find and update code patterns across a codebase
  • Data Cleaning: Normalize and clean data formats
  • URL Routing: Match URL patterns in web frameworks

Best Practices

Test Your Patterns

Always test regex patterns with various inputs, including edge cases, before using them in production. Use tools like this one to interactively test and debug your patterns.

Use Specific Patterns

Avoid overly broad patterns that might match unintended text. Be as specific as possible while still covering your use case.

Consider Performance

Some regex patterns can be slow, especially with long strings. Avoid catastrophic backtracking by using non-greedy quantifiers when appropriate and avoiding nested quantifiers.

Document Complex Patterns

Complex regex patterns can be difficult to understand. Add comments or documentation explaining what the pattern does and why it's structured that way.

Escape Special Characters

Remember to escape special regex characters (like ., *, +, ?) when you want to match them literally.

Use Character Classes

Use shorthand character classes like \d (digits), \w (word characters), and \s (whitespace) for readability and maintainability.

Frequently Asked Questions

What's the difference between global (g) and sticky (y) flags?

The g flag finds all matches in the string regardless of position. The y flag matches only at the specific position indicated by lastIndex. Sticky is useful for parsing text sequentially, while global finds all occurrences at once.

Why doesn't my regex match anything?

Common reasons include: escaping issues (special characters need backslashes), case sensitivity (use i flag), incorrect character classes, or the pattern doesn't account for whitespace/newlines. Use this tool to test your pattern step by step and see exactly what matches.

What's the difference between .* and .*?

.* is greedy—it matches as much as possible. .*? is non-greedy (lazy)—it matches as little as possible. For example, with text "a b c", a.*c matches "a b c" (greedy), while a.*?c also matches "a b c" but will stop at the first "c" it finds.

How do I match special characters literally?

Escape special characters with a backslash. For example, to match a literal period, use a backslash before the period. Special characters that need escaping include: period, asterisk, plus, question mark, caret, dollar, brackets, braces, parentheses, pipe, and backslash.

Is my regex data sent to a server?

No. All regex testing happens entirely in your browser using JavaScript. Your patterns and test text never leave your device. This tool is designed with privacy as a top priority.

Can I use this to learn regex?

Yes! This tool is perfect for learning regex. Try the example patterns, experiment with different flags, and see how changes to your pattern affect the matches. The explanation feature helps you understand what different parts of your pattern do.

What are capturing groups?

Capturing groups are parts of the pattern wrapped in parentheses (). They capture the matched text so you can use it later. For example, (\d3)-(\d4) captures the area code and number separately. Non-capturing groups (?:...) match but don't capture.