Regex Tester
What is a Regex Tester?
Writing a regular expression is trial and error: you adjust the pattern until it matches what you want and nothing else. A live tester shortens that loop. Type a pattern and sample text, and see every match, capture group and flag effect immediately, using the same JavaScript regex engine as browsers and Node.js.
How to use it
- Enter the regular expression without surrounding slashes.
- Set flags such as
g,iorm. - Paste sample text, including examples that should not match.
- Review highlighted matches and capture groups, and refine the pattern.
Worked example
Pattern (\w+)@(\w+\.\w+) with the g flag against Contact [email protected] or [email protected] finds two matches. For the first, group 1 is ana and group 2 is example.com. Changing to named groups, (?<user>\w+)@(?<domain>\w+\.\w+), makes the code that uses the result easier to read.
Testing tips
- Test negatives. Include strings that must fail. A pattern that matches everything is easy to write by accident.
- Anchor validations. Without
^and$,\d{4}also matches inside abc12345. - Watch the g flag in code. A global regex object remembers
lastIndex, so callingtest()twice can alternate between true and false. - Escape special characters such as
. * + ? ( ) [ ] { } | \ ^ $when you mean them literally.
Performance
Nested quantifiers like (a+)+ can cause catastrophic backtracking, freezing the page on certain inputs. This is the root of ReDoS vulnerabilities. Prefer specific character classes over .* and avoid nested repetition.
Frequently Asked Questions
Which regex flavour does the tester use?
JavaScript's built-in regex engine, the same one used by browsers and Node.js.
Why does my regex match only once?
Without the g flag, only the first match is returned. Enable g to find all matches.
Why does test() return true then false?
A regex with the g flag keeps a lastIndex between calls. Reset it or create a new regex for each test.
What is catastrophic backtracking?
Exponential slowdown caused by nested or overlapping quantifiers on certain inputs. It can freeze applications and cause ReDoS vulnerabilities.