Skip to content

Regex Tester

Test regular expressions with live match highlighting, group capture, and find & replace — all in your browser.

/ /
Test String No pattern
Matches

Enter a pattern and test string to see matches

Replace
Matches: 0 Groups: 0 Length: 0

Quick Regex Reference

. Any char (except newline)
\d Digit [0-9]
\w Word char [a-zA-Z0-9_]
\s Whitespace
^ Start of string
$ End of string
* 0 or more
+ 1 or more
? 0 or 1 (optional)
{n,m} Between n and m times
[abc] Character class
[^abc] Negated class
(abc) Capture group
(?:abc) Non-capture group
a|b Alternation (a or b)
\b Word boundary
(?=...) Lookahead
(?<=...) Lookbehind
\1 Backreference to group 1
$1 Replace with group 1

About the Regex Tester

A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Used in programming, text editors, and command-line tools, regex allows you to search, match, extract, and transform text with incredible precision.

This online regex tester lets you write and test regular expressions instantly with live highlighting. Enter your pattern, adjust flags (global, case-insensitive, multiline, dotall), and see matches highlighted in real time. Use the Replace field to test substitutions with $1, $2 group references.

Regex Flags Explained

  • g
    global
    Find ALL matches. Without g, only the first match is returned.
  • i
    case insensitive
    Match both uppercase and lowercase. /hello/i matches "Hello", "HELLO", and "hello".
  • m
    multiline
    ^ and $ match the start and end of each line, not just the whole string.
  • s
    dotall
    Makes . match newline characters (\n) in addition to all other characters.

Frequently Asked Questions

What is a regular expression?

A regular expression (regex) is a string of characters that defines a search pattern. It is used in programming languages, text editors, search tools, and databases to find, match, extract, validate, or replace text. Regex is supported in virtually every programming language including Python, JavaScript, Java, PHP, Go, Ruby, and more.

What does the "g" flag do in regex?

The "g" (global) flag tells the regex engine to find all matches in the string, not just the first one. Without "g", the regex stops after the first match. When using replace() in JavaScript, you need the "g" flag to replace all occurrences — without it, only the first match is replaced.

What is the difference between () and (?:) in regex?

() creates a capturing group that saves the matched text for later use via backreferences ($1, $2…) or group access. (?:) creates a non-capturing group — it groups the pattern for quantifiers or alternation but does not save the match. Use non-capturing groups when you don't need the captured text, for slightly better performance.

What does \b mean in regex?

\b is a word boundary assertion. It matches the position between a word character (\w) and a non-word character. For example, /\bcat\b/ matches "cat" in "the cat sat" but not "cats" or "concatenate". Word boundaries are useful for matching whole words without using spaces.

How do I match a literal dot (.) in regex?

In regex, . is a special metacharacter that matches any character except newline. To match a literal period, you must escape it with a backslash: \. For example, /3\.14/ matches "3.14" but not "3X14". Similarly, to match other metacharacters literally, prefix them with \: \*, \+, \?, \[, \], \(, \), \{, \}, \^, \$, \|, \\

What are lookahead and lookbehind?

Lookahead (?=...) matches a position followed by the pattern without including it in the match. Lookbehind (?<=...) matches a position preceded by the pattern. For example, /\d+(?= dollars)/ matches the number in "100 dollars" but the match is just "100", not "100 dollars". They are zero-width assertions — they check context without consuming characters.

Regex Tester: How to Build, Debug and Safely Use Regular Expressions in JavaScript

A regex tester lets you write a pattern, run it against sample text and see exactly which parts match. This one uses your browser's own JavaScript regular expression engine, so what matches here will match in your JavaScript code. This guide gives you a repeatable debugging method, tested examples and the pitfalls that cause most regex bugs.

Test a Pattern in Five Steps

  1. Paste realistic sample text, including cases that should not match.
  2. Type your pattern between the slashes.
  3. Toggle flags: g (all matches), i (ignore case), m (multiline) and s (dot matches newline).
  4. Check the highlighted matches and the captured groups below.
  5. Use the replace field to preview a substitution, then copy the final pattern into your code.

Build it up gradually. Start with the simplest piece that matches something, then add one element at a time. When it stops matching, the last thing you added is the cause.

Worked Examples With Verified Results

Each of these was run in a JavaScript engine to confirm the output.

1. Extract and reorder a date

Pattern:  (\d{4})-(\d{2})-(\d{2})
Text:     Due 2026-11-07 and 2027-01-15
Matches:  2026-11-07, 2027-01-15
Groups:   $1=2026  $2=11  $3=07

Replace:  $3/$2/$1
Result:   07/11/2026 15/01/2027

2. Greedy vs lazy

Text:     <b>x</b><b>y</b>

<b>.*</b>    matches the whole string  <b>x</b><b>y</b>
<b>.*?</b>   matches only the first    <b>x</b>

The quantifier * grabs as much as it can. Adding ? makes it grab as little as possible.

3. Named groups and lookarounds

(?<year>\d{4})-(?<month>\d{2})   on "2026-11"  gives year=2026, month=11
\d+(?=USD)                      on "45USD 30EUR" matches 45 only
(?<=EUR)\d+                     on "USD45 EUR30" matches 30 only
\$(\d+(?:\.\d{2})?)             on "price: $45.99, $3" matches $45.99 and $3

Regex Syntax Cheat Sheet

TokenMeaningExample
.Any character except a newline (all, with s)a.c matches "abc"
\d \w \sDigit, word character, whitespace\d+ matches "2026"
\D \W \SThe opposites\S+ matches a word
[abc] / [^abc]Any of / none of these characters[^,]+ matches a CSV field
* + ?0 or more, 1 or more, 0 or 1colou?r
{n,m}Between n and m repeats\d{2,4}
^ $Start / end of string (or line with m)^Hello
\bWord boundary\bcat\b skips "concat"
( ) / (?: )Capturing / non-capturing group(?:ab)+
a|bAlternationcat|dog
(?= ) (?! )Lookahead positive / negative\d+(?!px)
(?<= ) (?<! )Lookbehind positive / negative(?<=\$)\d+

The authoritative reference is the MDN regular expressions guide.

Bugs You Will Meet (and the Fixes)

The ^ and $ anchors do not match each line

On the text line1 and line2 separated by a newline, ^line\d$ with only g finds nothing. Turn on m and it returns both lines. Without m, the anchors match only the start and end of the whole string.

The dot stops at line breaks

The pattern a.b will not match "a" newline "b" until you add the s flag.

Empty matches

The pattern a* can match zero characters. Replacing it globally in "aaa" with X gives "XX", not "X": one for "aaa", one for the empty string at the end. Use a+ if you require at least one.

The g flag and lastIndex in code

In JavaScript, a regex object with g remembers its position. Calling .test() repeatedly on the same object alternates between true and false:

const g = /a/g;
g.test("aa");  // true  (lastIndex 1)
g.test("aa");  // true  (lastIndex 2)
g.test("aa");  // false (lastIndex reset to 0)

Drop the g flag for yes/no checks, or create a new regex each time.

Unicode and emoji

/^.$/ fails on a single emoji because the emoji is two UTF-16 code units. The u flag fixes that in code: /^.$/u matches. This tester exposes g, i, m and s, so for emoji handling add u in your own code and re-test there.

Catastrophic Backtracking (ReDoS)

Some patterns can freeze a page. Nested quantifiers such as (a+)+b force the engine to try an exponential number of ways to split the input when the match fails. A short hostile string can lock up a browser tab or a server thread.

OWASP documents this class of attack as Regular expression Denial of Service. To stay safe, avoid nested repetition, keep alternatives from overlapping and limit input length before running a regex on user-supplied data. If this tester hangs while you type, that is usually the cause: delete the last change.

Practical Patterns, With Caveats

GoalPatternCaveat
Simple email check^[\w.+-]+@[\w-]+\.[\w.]+$Accepts some invalid addresses. Confirm by sending a message
Hex color^#(?:[0-9a-f]{3}){1,2}$ with iDoes not cover 4- or 8-digit forms with alpha
Whitespace trim^\s+|\s+$ with gReplace with an empty string
Collapse spaces {2,}Replace with a single space
Find URLshttps?:\/\/[^\s]+May capture trailing punctuation

Regex is a poor fit for nested structures like HTML or JSON. Use a parser. For text cleanup that does not need a pattern, the duplicate line remover or line break remover are simpler. To match against encoded data, decode it first with the URL encoder/decoder.

A Debugging Routine for Regex That Will Not Match

When a pattern misbehaves, guessing makes it worse. Work through these checks in order.

  1. Confirm the flags. Missing g returns one match. Missing i makes "Error" and "error" differ. Missing m breaks line anchors.
  2. Look for invisible characters. A tab, a non-breaking space or a carriage return in your sample text will defeat a pattern that uses a literal space. Use \s when whitespace may vary.
  3. Check your escapes. A literal dot, parenthesis, plus, question mark or dollar sign needs a backslash. 3.14 also matches "3x14"; 3\.14 does not.
  4. Simplify. Remove the second half of the pattern. If it matches now, the removed part is the bug.
  5. Test negatives. Add strings that must not match. A pattern that matches everything passes every positive test.

Anchoring and validation

Without ^ and $, a validation pattern only needs to match somewhere in the input. The pattern \d{4} accepts "abc12345xyz". If you are validating a whole field, anchor both ends.

Strings in code need double escaping

Inside a normal JavaScript string, the backslash is itself an escape character. To build a regex from a string, write new RegExp("\\d+"), not new RegExp("\d+"). A regex literal such as /\d+/ avoids the problem. When you copy a working pattern from the tester into a string, double every backslash.

Performance habits

  • Be specific. [^,]* is faster and safer than .* when you know the delimiter.
  • Anchor patterns when you can, so the engine does not retry from every position.
  • Prefer non-capturing groups (?:...) when you do not need the captured text.

More questions

Why does my regex work here but fail in Python or PHP?

Regex dialects differ. This tester uses JavaScript's engine. Named groups are written (?<name>...) in JavaScript but (?P<name>...) in Python, and some engines support features JavaScript lacks, such as possessive quantifiers.

What is the difference between match and test?

test() returns true or false. match() and matchAll() return the matched text and groups. With the g flag, match() returns only full matches and drops the groups, while matchAll() keeps them.

How do I reference a captured group in the replacement?

Use $1, $2 and so on. For named groups use $<name>. Use $& for the whole match and $$ for a literal dollar sign.

Should I escape forward slashes in a pattern?

Inside the tester, no. In a JavaScript regex literal such as /a\/b/ you must escape them. When you build a pattern from a string with new RegExp("a/b"), no escape is needed.

How do I match a pattern across multiple lines?

Combine flags to fit your need. Use m to anchor on lines, s so the dot crosses newlines, or use [\s\S]*? as a portable "anything, including newlines" fragment.

Is my test text sent anywhere?

No. Matching runs on your device using the browser's engine, so sample data such as logs or customer records stays local.