How to Test a Regular Expression
By Tools Star Hub Editorial Team · Published · Updated
A regular expression is a pattern that searches text. The Regex Tester on this site uses JavaScript regular expressions. The pattern \d+ with the g flag finds 12 and 4 in Room 12 and room 4. The first match starts at index 5. The second starts at index 17. The page shows up to 50 matches and rejects a pattern longer than 300 characters.
A pattern that looks right in your head can miss a digit, stop too early, or match more than you wanted. Testing it against a real sample shows the matches and their positions before you put the pattern in code. The flags change that result. Guessing the flags is a common way to get a different match count.
Which flags does this tester allow?
Only g, i, m, s, and u. Any other flag is rejected with Use only the flags g, i, m, s, and u. The g flag finds every match. Without it, a search in JavaScript would stop after one match, but this page still scans with a global copy so it can list matches. The global marker on the result tells you whether g was selected.
i ignores letter case. m changes how ^ and $ treat line starts and ends. s lets a dot match a newline. u turns on Unicode mode. Leave a flag off unless you need that behavior. Extra flags are a different pattern.
What does a match report include?
Each match shows the text that matched and the index where it starts. Indexes are zero-based. In Room 12 and room 4, the digits 12 start after Room and a space, which is index 5. The digit 4 starts at index 17.
Parentheses create groups. Named groups are listed by name. The tester caps the list at 50 matches and marks the result truncated when the text has more. The sample text itself can be up to 100,000 characters. A longer paste is rejected.
What should you avoid in a test pattern?
An empty pattern is rejected. An invalid pattern returns That pattern is not valid, plus the browser’s syntax message. Fix the pattern. The page does not auto-correct it.
A pattern with nested quantifiers can be slow even on a short sample. If the page struggles, shorten the sample and simplify the pattern. Do not paste secrets you do not want on screen. The test runs locally, but the text is still visible in the tab.
A match index counts characters from 0. The word Room is indexes 0 through 3, the space is index 4, and 12 starts at index 5. If your editor counts from 1, add one when you compare the two displays. The tester does not rewrite the sample to highlight matches in place.
How to test a regular expression
- Enter the pattern without slashes. Type \d+, not /\d+/. The slashes are a JavaScript literal, not part of the pattern this box expects.
- Turn on only the flags you need. g lists every match. i ignores case. m, s, and u change anchors, the dot, and Unicode handling.
- Paste a realistic sample. Use text that should match and text that should not. A sample of only digits will not show you a false match.
- Read the index and the groups. The index is where the match starts. Groups show what each pair of parentheses captured.
Example
Digits in a sentence
\d+ with g on Room 12 and room 4 returns two matches: 12 at index 5, and 4 at index 17. room has no digits, so it is not a match.
An invalid pattern
A lone opening bracket is not a valid pattern. The page says that pattern is not valid and includes the syntax message. It does not return a partial match.
Common mistakes
Slashes are not part of the pattern
Copying /\d+/g from code includes the slashes. This tester wants the pattern and the flags in separate controls.
Fifty matches is the cap
A long list stops at 50 and is marked truncated. Narrow the sample or the pattern if you need a later match.
FAQ
- Where are the numbers in Room 12 and room 4?
- \d+ with the g flag matches 12 at index 5 and 4 at index 17. That is two matches.
- Which flags can I use?
- g, i, m, s, and u. Any other letter is rejected.
- How long can the pattern and the text be?
- The pattern can be 300 characters. The sample can be 100,000 characters. The match list stops at 50.
- Does the tester fix a broken pattern?
- No. An empty pattern asks you to enter one. An invalid pattern returns the syntax error.
- Is the sample uploaded?
- No. The test runs in the browser.
To try a JavaScript pattern against a sample, use the Regex Tester. It lists matches, indexes, and groups.
If you need to swap text after the pattern works, see How to find and replace text.
Patterns often clean structured text. See What is JSON?.