Test both matches and non-matches
A good regex test set must include values that should match and values that should not match.
Testing only valid examples can hide serious mistakes, because an overly permissive regex may accept almost anything.
For every pattern, prepare examples that prove the regex accepts the intended format and rejects malformed input.
Use realistic input
Regex patterns often fail when they meet real user input instead of clean artificial examples.
Users may add spaces, line breaks, accents, mixed casing, separators or incomplete values.
Testing with realistic input helps you decide whether the regex should be strict, flexible or combined with a normalization step.
Include edge cases
Edge cases are values close to the boundary of what the regex should accept.
For example, test the shortest valid value, the longest valid value, one character too short and one character too long.
Edge cases reveal off-by-one errors, missing anchors and quantifiers that are too broad or too narrow.
Test anchors carefully
Anchors change the meaning of a regex dramatically.
A pattern without anchors may find a valid fragment inside a larger invalid string, while an anchored pattern validates the whole input.
When testing validation regexes, always include examples with extra characters before or after the expected value.
Test flags and modes
Regex flags can completely change the result of a pattern.
Case-insensitive mode, multiline mode and global matching can all affect what is matched and how results are returned.
When documenting or deploying a regex, test it with the exact flags that will be used in production.
Test extraction separately from validation
Validation and extraction are different tasks.
A validation regex usually checks the whole input, while an extraction regex searches for a value inside a larger text.
Do not reuse the same test cases blindly. Extraction patterns should be tested inside sentences, logs, pasted text and noisy data.
Test replacement patterns
When a regex is used for replacement, the match itself is only half of the test.
You should also verify the final output after replacement, especially when using capture groups, lookarounds or backreferences.
Replacement tests are important for masking, cleaning, formatting and transforming text safely.
Test in the target regex engine
Different regex engines support different features and sometimes interpret syntax differently.
A pattern that works in PCRE may not work in JavaScript, and replacement syntax may vary between languages.
Always test production regexes in the same engine, language and runtime where they will actually run.
Keep a reusable test suite
Important regexes should have a reusable test suite, not just a few manual checks.
Store valid examples, invalid examples and edge cases next to the code or documentation where the regex is maintained.
This makes future changes safer, because you can immediately see whether a modification improves the pattern or breaks existing behavior.