Regex Tester

Test, debug, and learn regular expressions with real-time matching and highlighting.

Regular Expression

//

Matches Found

2

Highlighted Matches

Contact us at hello@example.com or support@company.org for assistance. Invalid: notanemail@

Match Details

"hello@example.com"Index: 14
"support@company.org"Index: 35

Replaced String

Contact us at [EMAIL] or [EMAIL] for assistance. Invalid: notanemail@

What Is a Regular Expression?

A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Developers, data analysts, and system administrators use regex to find, validate, extract, and replace text inside strings, log files, databases, and source code. The pattern is matched against a target string by a regex engine — in this tool, the JavaScript engine built into your browser.

Regex syntax originates from formal language theory and was popularized in Unix tools like grep, sed, and awk in the 1970s. Today every major programming language — JavaScript, Python, Java, Go, Rust, PHP, Ruby — ships a regex engine, and the syntax is largely compatible across all of them.

A pattern like \b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b matches most standard email addresses. Breaking it down: \b anchors to word boundaries, [A-Za-z0-9._%+-]+ matches the local part (one or more allowed characters), @ is literal, [A-Za-z0-9.-]+ matches the domain label, \. matches a literal dot, and [A-Z|a-z]{2,} requires a TLD of two or more letters.

This online regex tester lets you write a pattern, choose flags, paste a test string, and instantly see every match highlighted in yellow along with its character index and any named or numbered capture groups. You can also specify a replacement string and preview the full substituted output — all in real time without sending data to any server.

How Regex Matching Works

Under the hood this tool calls new RegExp(pattern, flags) to compile your pattern, then runs the engine against your test string. When the global flag (g) is active, the engine loops via regex.exec(testString) until no further match is found, collecting each match object — its full text, start index, and any capture groups. Without the global flag, only the first match is returned.

Replacement uses the native String.prototype.replace(regex, replacement) method. Special replacement tokens such as $1, $2 refer to numbered capture groups, and $& inserts the full matched text — the same convention used by JavaScript, Java, and most PCRE-compatible engines.

If the pattern is syntactically invalid (for example an unclosed bracket [), the JavaScript engine throws a SyntaxError. This tool catches that error and displays the engine's message below the pattern field so you can diagnose the problem immediately.

Regex Flags Explained

Flag Letter Effect
GlobalgFind all matches, not just the first
Case-insensitiveiTreat uppercase and lowercase as equal
Multilinem^ and $ match start/end of each line, not just the whole string
Dotalls. matches newline characters as well

Regex Matching Model

matches = []; while ((m = /pattern/flags.exec(str)) !== null) matches.push(m)

Where:

  • pattern= The regular expression pattern string
  • flags= Modifier flags: g (global), i (case-insensitive), m (multiline), s (dotall)
  • str= The test string being searched
  • m= A match object: { [0]: full match, [1..n]: capture groups, index: start position }
  • matches= Array of all match objects found (empty array if no matches)

Regex Syntax Quick Reference

Regex syntax can look intimidating at first, but most patterns are built from a small set of building blocks. The table below covers the constructs you will use most often when testing patterns with this tool.

Token Meaning Example
.Any single character (except newline by default)a.c matches "abc", "a1c"
^Start of string (or line with m flag)^Hello
$End of string (or line with m flag)world$
*Zero or more of the preceding tokenab*c matches "ac", "abc", "abbc"
+One or more of the preceding tokenab+c matches "abc", "abbc" but not "ac"
?Zero or one (optional)colou?r matches "color" and "colour"
{n,m}Between n and m repetitions\d{2,4} matches 2–4 digits
[abc]Character class — any one of a, b, or c[aeiou] matches any vowel
[^abc]Negated class — any character except a, b, or c[^\d] matches non-digits
(abc)Capture group — stores the matched text(\d{4}) captures a four-digit year
(?:abc)Non-capturing group(?:foo|bar)
\dAny digit (0–9)\d+ matches "42" or "2024"
\wWord character (letter, digit, underscore)\w+ matches "hello_world"
\sWhitespace (space, tab, newline)\s+ collapses runs of whitespace
\bWord boundary\bcat\b matches "cat" but not "concatenate"
a|bAlternation — matches a or bjpg|png|gif

Common Regex Patterns and Use Cases

The built-in pattern library in this regex tester covers the patterns developers reach for most often. Here is an expanded reference with the full patterns and when to use each one.

Email Address Validation

Pattern: \b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b

This pattern is suitable for client-side pre-validation. It allows dots, plus signs, hyphens, underscores, and percent signs in the local part — all legal per RFC 5321. It does not cover every edge case of the full RFC (such as quoted strings), but it catches the vast majority of typos.

URLs

Pattern: https?:\/\/[^\s]+

Matches any URL starting with http:// or https:// and running until the next whitespace. Use this to extract links from plain text logs or messages.

US Phone Numbers

Pattern: \(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}

Matches formats like (555) 867-5309, 555-867-5309, and 5558675309. The \(? and \)? make the area code parentheses optional.

IP Addresses

Pattern: \b(?:\d{1,3}\.){3}\d{1,3}\b

Matches any four-octet numeric sequence separated by dots. Note this does not validate that each octet is ≤ 255 — further validation in application code is recommended.

ISO Dates

Pattern: \d{4}-\d{2}-\d{2}

Matches YYYY-MM-DD format as used in databases, JSON APIs, and log files. Combine with ^ and $ anchors if the date must occupy its own field.

Hex Color Codes

Pattern: #[0-9A-Fa-f]{6}\b

Matches six-digit CSS hex colors. Extend to #[0-9A-Fa-f]{3,8}\b to also match three-digit shorthand and eight-digit RGBA variants.

Replacement Strings and Capture Groups

The replacement feature in this tool calls JavaScript's String.prototype.replace(regex, replacement). This is more powerful than a simple find-and-replace because the replacement string can reference parts of each match through special tokens.

  • $& — inserts the entire matched substring.
  • $1, $2, … — inserts the text captured by the first, second, … capture group in the pattern.
  • $` — inserts the portion of the string before the match.
  • $' — inserts the portion of the string after the match.

For example, if your pattern is (\d{4})-(\d{2})-(\d{2}) and your replacement is $2/$3/$1, then the date 2025-06-07 becomes 06/07/2025 — converting from ISO format to US format in a single substitution.

When the global flag is active, every match in the test string is replaced. Without it, only the first match is substituted. This mirrors how JavaScript's String.replace behaves natively.

Capture groups are also shown individually in the Match Details panel. Each match card lists the captured substrings as $1, $2, and so on. This is invaluable when debugging complex patterns that extract structured data — for example, parsing dates, parsing log lines, or splitting a full name into first and last parts.

Worked Examples

Extract All Email Addresses

Problem:

Given the string "Contact hello@example.com or admin@site.org", extract all email addresses.

Solution Steps:

  1. 1Set pattern to: \b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b
  2. 2Set flags to "gi" so the match is case-insensitive and finds all occurrences.
  3. 3Paste the test string into the Test String field.
  4. 4The engine executes new RegExp(pattern, "gi") and loops with regex.exec() until no more matches remain.
  5. 5Result: 2 matches — "hello@example.com" (index 8) and "admin@site.org" (index 30).

Result:

2 matches found: ["hello@example.com", "admin@site.org"]

Replace Dates from ISO to US Format Using Capture Groups

Problem:

Convert "Order placed on 2025-06-07 and shipped 2025-06-10" to use MM/DD/YYYY format.

Solution Steps:

  1. 1Set pattern to: (\d{4})-(\d{2})-(\d{2})
  2. 2Set flags to "g" to replace all occurrences.
  3. 3Set replacement to: $2/$3/$1
  4. 4The engine matches "2025-06-07": group $1="2025", $2="06", $3="07". Replacement becomes "06/07/2025".
  5. 5It then matches "2025-06-10": $1="2025", $2="06", $3="10". Replacement becomes "06/10/2025".

Result:

"Order placed on 06/07/2025 and shipped 06/10/2025"

Validate and Redact US Phone Numbers

Problem:

Find all US phone numbers in a customer note and replace them with "[PHONE]" for privacy.

Solution Steps:

  1. 1Set pattern to: \(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
  2. 2Set flags to "g".
  3. 3Set test string to: "Call me at (555) 867-5309 or 555.123.4567 anytime."
  4. 4Set replacement to: [PHONE]
  5. 5The engine finds two matches: "(555) 867-5309" and "555.123.4567" and substitutes both.

Result:

"Call me at [PHONE] or [PHONE] anytime." — 2 matches replaced.

Match Lines Starting With a Word (Multiline Mode)

Problem:

In a multi-line log file, find every line that starts with "ERROR".

Solution Steps:

  1. 1Set pattern to: ^ERROR.*
  2. 2Set flags to "gm" (global + multiline so ^ matches the start of each line).
  3. 3Paste a log excerpt with mixed INFO, WARN, and ERROR lines.
  4. 4Without the m flag, ^ only matches the very start of the entire string, so most ERROR lines would be missed.
  5. 5With m flag active, the engine treats each newline as a line boundary and matches every line beginning with "ERROR".

Result:

All ERROR-prefixed lines highlighted; INFO and WARN lines ignored.

Tips & Best Practices

  • Use the "Common Patterns" buttons to load a working email, URL, or phone pattern instantly — then modify it for your specific needs.
  • Enable the Case-Insensitive (i) flag when matching user input where capitalization may vary, such as searching for a keyword in a comment field.
  • Test patterns against edge cases as well as valid examples: an empty string, a string with no matches, and a string where the pattern appears at the very start or end.
  • Use non-capturing groups (?:...) when you need grouping for alternation or repetition but do not need to reference the group in a replacement — this keeps your $1, $2 references clean.
  • Prefix quantifiers with ? to make them lazy (e.g., +? instead of +) when you need to match the shortest possible string between two delimiters.
  • Always test your pattern with the global flag enabled if you plan to use it in a loop or a replace-all operation — behavior can differ significantly from the single-match case.
  • Use \b word boundaries to avoid partial-word matches; \bcat\b matches "cat" but not "catalog" or "bobcat".
  • When building a pattern incrementally, start simple and add complexity one token at a time, verifying each step in the highlighted match view before continuing.

Frequently Asked Questions

Without the global (g) flag the regex engine returns only the first match it finds and stops. With the g flag the engine continues scanning from where the last match ended, collecting every non-overlapping occurrence. In this tool the Match Count display and the replacement output both reflect this: with g you see all matches replaced, without g only the first.
The most common culprits are greedy vs. lazy quantifiers and missing anchors. By default <code>.*</code> is greedy — it consumes as many characters as possible before backtracking. Append <code>?</code> to make it lazy: <code>.*?</code> matches as few characters as possible. If your pattern is matching too broadly, add word-boundary anchors <code>\b</code> or line anchors <code>^</code> and <code>$</code> to constrain where the match can occur.
This tool uses the JavaScript (ECMAScript) regex engine. JavaScript regex is very close to PCRE (Perl-Compatible Regular Expressions) used by Python, PHP, and many other languages. The main differences are that JavaScript does not support lookbehind of variable length (prior to ES2018), does not use the VERBOSE (?x) flag, and uses a slightly different named-capture syntax. Most common patterns — email, URL, phone, date — will work identically across languages.
Metacharacters like <code>. * + ? ^ $ { } [ ] | ( ) \</code> have special meaning in regex. To match them literally, prefix them with a backslash. For example, <code>\.</code> matches a literal dot instead of "any character". In JavaScript string literals you also need to double the backslash: <code>"\\."</code> in source code represents the pattern <code>\.</code>.
A capture group is a portion of the pattern wrapped in parentheses, e.g. <code>(\d{4})</code>. The text matched by that group is stored and can be referenced in the replacement string as <code>$1</code> for the first group, <code>$2</code> for the second, and so on. This lets you reorder or reformat matched content without losing it — for example, swapping year and month in a date string.
If the engine cannot parse your pattern it throws a SyntaxError and the tool displays the engine message in red below the pattern field. Common causes include unmatched brackets (<code>[</code> without <code>]</code>), unmatched parentheses, invalid escape sequences, and quantifiers without a preceding token (e.g. <code>+</code> at the start). Fix the syntax and the error message will disappear.
No. All matching and replacement runs entirely in your browser using the JavaScript built-in RegExp engine. Your pattern and test string are never transmitted anywhere. This makes the tool safe to use with sensitive data such as log excerpts, customer records, or API responses.

Sources & References

Last updated: 2026-06-05

💡

Help us improve!

How would you rate the Regex Tester?

<>

Editorial Note

MyCalcBuddy Editorial Team

This page is maintained as an educational calculator reference.

Source

Formula Source: Standard Mathematical References

by Various

UpdatedLast reviewed: May 2026
CheckedFormula checks are based on standard references and internal QA review.