How to test a regular expression
- Type your pattern in the Pattern box. Leave out the slashes.
- Switch flags on or off with the buttons under it. The g flag is on by default so every match is found.
- Paste or type your Test text. Matches are highlighted as you type, and the list below shows each match with its position and capture groups.
If the pattern is not valid, a plain error message appears instead of the results.
What the results show
Each match is listed with the text it found and where it starts and ends. Capture groups, the parts of the pattern in parentheses, are shown under their match, with their names if you used the (?<name>...) form. A group that did not take part in the match, for example the unused side of an alternation, is marked as such.
Highlights alternate between two styles so that neighbouring matches are easy to tell apart. Empty matches, which happen with patterns like x*, are counted and listed but not highlighted.
Why matching has a timeout
Regular expressions can go wrong in a special way. A pattern with nested repeats, such as (a+)+$, may need billions of steps on a string that almost matches. In a normal page this would freeze the tab. Here the pattern runs in a separate Web Worker, and if it has not finished after one second the worker is stopped and you see the message “Pattern took too long”. You can then edit the pattern and try again without reloading.
To avoid slow patterns, avoid nesting quantifiers, make alternatives that cannot overlap, and anchor patterns with ^ and $ when you can.
Tips
- Use
\bfor word boundaries and\d,\w,\sfor digits, word characters and spaces. - Make quantifiers lazy with
?(.*?) when you want the shortest match. - Test with edge cases: empty text, very long text and text with line breaks.
- Once your pattern works, copy it into your code. Remember to double the backslashes inside a normal JavaScript string.
- Working with data? Format a query with the SQL formatter or turn notes into a page with Markdown to HTML.
Your pattern and text stay on your device. There is nothing to sign up for and nothing is stored.