Regex for QA Testers — Validate Form Inputs and Test Patterns Faster
Table of Contents
As a QA tester or SDET, regex serves two purposes: understanding the validation rules the dev team implemented (so you can write comprehensive test cases), and testing regex patterns yourself when you're writing automated tests with Selenium, Playwright, Cypress, or pytest.
This guide covers both angles — how to reverse-engineer validation rules from regex, how to generate test cases that break validators, and how to write regex in your own test code.
How to Read a Developer's Validation Regex and Write Test Cases From It
When a dev hands you a regex for field validation, the browser tester lets you understand exactly what it accepts and rejects — without asking them to explain it.
Step by step:
- Paste the pattern into the tester
- Test obvious valid values — does a normal email address match? A standard phone number? A typical password?
- Find the boundaries — what is the minimum and maximum length? What characters are on the edge of acceptance?
- Test boundary conditions — one character under the minimum, one over the maximum, first and last valid character in a range
- Test invalid patterns — empty string, spaces only, special characters the pattern seems to reject
- Document what you found — this becomes your equivalence partitioning and boundary value analysis test matrix
For example, if the email regex is ^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$, you can immediately see: no spaces allowed, the + symbol is allowed, the TLD must be at least 2 characters. Write test cases for all of these.
Generating Test Cases from Validation Regex — A Systematic Approach
For any validation regex, generate at least these test categories:
Valid partition (should pass):
- Minimum valid length
- Maximum valid length
- All allowed characters used at least once
- Real-world examples from production data if available
Invalid partition (should fail):
- Empty string
- One character below minimum length
- One character above maximum length (if applicable)
- First character outside the allowed range
- Last character outside the allowed range
- Character that looks allowed but is not (e.g., semicolon when only
@$!%are special characters)
Edge cases:
- Leading whitespace
- Trailing whitespace
- Unicode characters (emoji, CJK, Arabic)
- SQL injection strings (
' OR '1'='1) - XSS strings (
<script>alert(1)</script>) - Null character (
\0) - Very long string (10,000+ characters)
Paste each test value into the browser tester and verify the match result matches what you expect the application to do. Inconsistencies reveal bugs in the validation rule.
Sell Custom Apparel — We Handle Printing & Free ShippingWriting Regex in Selenium, Playwright, and Cypress Tests
Playwright (Python):
import re
# Assert that the text matches a pattern
expect(page.locator('.price')).to_have_text(re.compile(r'\$[0-9,]+\.\d{2}'))
# Wait for a URL matching a pattern
page.wait_for_url(re.compile(r'/checkout/success/\d+'))
Playwright (JavaScript/TypeScript):
// Assert element text matches pattern
await expect(page.locator('.status')).toHaveText(/Order #\d{6} confirmed/);
// Navigate and check URL
await expect(page).toHaveURL(/\/user\/\d+\/profile/);
Pytest with regex assertions:
import re
assert re.match(r'^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$', order_id)
Test all of these patterns in the browser tester with representative values before adding them to test code. A regex assertion that always passes because the pattern is wrong will silently let bugs through your test suite.
Using Regex to Validate API Response Fields
API tests often need to assert that response fields match expected formats without specifying exact values. Regex is the right tool:
Validate an order ID format: ^ORD-\d{6}$
Test with: ORD-123456 (pass), ORD-12345 (fail — too short), ORD-ABCDEF (fail — not digits)
Validate a timestamp format: ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z?$
Validate a currency amount: ^\d+\.\d{2}$
Test: 12.99 (pass), 12.9 (fail), 12 (fail)
Validate a status code value: ^(pending|processing|shipped|delivered|cancelled)$
In Postman, these go in the Tests tab:
pm.test("Order ID format", () => { pm.expect(pm.response.json().orderId).to.match(/^ORD-\d{6}$/); });
In REST Assured (Java):
.body("orderId", matchesPattern("^ORD-\d{6}$"))
Building a Test Matrix from a Validation Regex
When a developer sends you this password validation regex: ^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Here is how to build a complete test matrix:
| Test Case | Input | Expected | Why |
|---|---|---|---|
| All rules met | Password1! | Pass | Baseline valid |
| No uppercase | password1! | Fail | Missing [A-Z] |
| No lowercase | PASSWORD1! | Fail | Missing [a-z] |
| No digit | Password!! | Fail | Missing \d |
| No special char | Password11 | Fail | Missing [@$!%*?&] |
| Too short (7 chars) | Pass1!A | Fail | Below {8,} minimum |
| Exactly 8 chars | Pass1!Ab | Pass | At minimum length |
| Unlisted special char | Password1# | Fail | # not in [@$!%*?&] |
| Spaces | Pass word1! | Fail | Space not in character class |
| Empty string | Fail | Below minimum length |
Test each row in the browser tester before adding it to your test plan. This confirms your expected results are correct — not just your assumptions.
Try It Free — No Signup Required
Runs 100% in your browser. No data is collected, stored, or sent anywhere.
Open Free Regex TesterFrequently Asked Questions
How do I know if a field accepts a certain character without reading the code?
Paste the validation regex into the browser tester and test with that specific character. If the field's regex highlights a match for your test value, the character is accepted. If not, it will be rejected by the validator.
Can I use the regex tester to understand what test cases to write?
Yes. Paste the developer's regex and systematically test boundary conditions: minimum length, maximum length, allowed special characters, first/last character in each range, and invalid inputs. The match/no-match results directly map to pass/fail expectations in your test cases.
How do I write regex assertions in Playwright?
Playwright accepts regex directly in expect assertions. In Python: expect(locator).to_have_text(re.compile(pattern)). In JavaScript: await expect(locator).toHaveText(/pattern/). Test the pattern in the browser tester first to confirm it matches the expected text format.

