Regex tester
Test regular expressions in your browser with live match highlighting, capture groups, replacement preview, sample patterns, and a compact regex cheat sheet.
Capture groups
About the Regex tester
This regex tester lets you build a JavaScript regular expression and see exactly what it matches against your own sample text, in real time. It highlights every match in place, lists the capture groups for each one, previews a search-and-replace, and reports syntax errors as you type. Instead of guessing whether a pattern works, you watch it work, which turns regex from trial-and-error into something you can reason about.
Use it to design a pattern for validation, extraction, or a find-and-replace, to debug one that is matching too much or too little, or to learn how a feature like a capture group or a lazy quantifier behaves. It uses the same regex engine as JavaScript, so a pattern that works here behaves the same in your Node or browser code.
How to use it
- Type your pattern into the Pattern field, or pick a ready-made one from the Samples menu.
- Toggle the flags you need: g (global), i (ignore case), m (multiline), s (dot matches newline), u (unicode).
- Paste the text you want to test against into the Test text panel.
- Read the live match count, the highlighted matches, and the capture-group breakdown for each hit.
- Type a replacement using $1, $2 backreferences to see a live replace preview.
- Open the cheat sheet for a quick reference, and copy it if you want it handy.
Examples
Extract the parts of an email
With the pattern \b(\w+)@(\w+\.\w+)\b and the i and g flags on, testing 'dev@example.com' highlights the whole address and shows capture group $1 as 'dev' and $2 as 'example.com'. The two parentheses split the match into a local part and a domain you can reuse.
Reformat with a replace pattern
Keep the email pattern and set the replacement to '$1 at $2'. The preview rewrites 'support@phrasit.com' as 'support at phrasit.com'. Backreferences let you reorder and reword captured text, which is the basis of structured find-and-replace.
See why global matters
Test a pattern against text with several matches. With g off, only the first match is found and counted. Turn g on and every occurrence highlights. The tester also steps past zero-width matches safely, so an empty match cannot freeze the count in a loop.
Frequently asked questions
- What do the five flags do?
- g finds all matches instead of just the first; i makes matching case-insensitive; m makes ^ and $ match at every line boundary, not only the string ends; s lets the dot match newline characters too; u enables full Unicode handling of the pattern.
- How do capture groups and $1 work?
- Parentheses create a capture group, and each group is numbered left to right by its opening bracket. The tester lists what each group captured per match, and in the replace field $1, $2, and so on insert those captured pieces into the output.
- Why does my replacement only change the first match?
- Replace follows the global flag. Without g, only the first match is substituted; turn g on to replace every occurrence. This mirrors how String.replace behaves in JavaScript with and without the global flag.
- Is this the same regex syntax as my code?
- Yes, it is the JavaScript RegExp engine, so the same pattern and flags work identically in Node and in the browser. Note that some constructs from other languages, like lookbehind in older runtimes or possessive quantifiers, may differ from Perl or PCRE.
- What does 'Invalid pattern' mean?
- Your pattern is not syntactically valid, often an unbalanced bracket or parenthesis, a stray quantifier, or an unterminated group. The tester shows the engine's error message so you can find the offending piece before relying on the pattern.
Good to know
Two habits make regular expressions far less painful. Match against real, messy sample text rather than the one clean string you have in mind, because edge cases (empty fields, odd spacing, unexpected characters) are where patterns quietly fail. And prefer non-capturing groups (?:...) when you only need grouping, so your numbered captures stay meaningful and your replace strings do not shift.
Know the limits. It uses the JavaScript engine, so behaviour matches JavaScript but not necessarily the Perl or PCRE dialects you may copy patterns from. Greedy quantifiers like .* match as much as possible and are a classic cause of a pattern eating too much; the lazy form .*? matches as little as possible. And regex is the wrong tool for parsing deeply nested or recursive structures such as HTML or arbitrary JSON, where a real parser is both correct and safer. Use it for the flat, pattern-shaped problems it excels at.