A regular expression can find a pattern in text, but a match is not proof that the input is valid for your business rule. A pattern that recognizes an email-shaped string may accept an address that cannot receive mail, while a strict pattern may reject a legitimate unusual address. GoldieKit's Regex tester runs bounded JavaScript expressions in a timed worker and shows captures. Use it to test behavior against examples and counterexamples, then decide whether regex is the right tool for the larger task.
Write the rule in plain language first
Before writing symbols, state exactly what should match. “A six-digit code” might mean six ASCII digits and nothing else, or it might mean six digits somewhere inside a message. Anchors change that distinction. ^\d{6}$ aims to match the whole string, while \d{6} can find a sequence inside a longer one. In JavaScript, \d matches ASCII digits in common regex behavior; if you need digits from other scripts, verify engine support and use an explicit Unicode strategy.
List at least three accepted and three rejected examples. For the six-digit code: accept 123456; reject 12345, 1234567, and abc123456. Add an example with a newline if the input can contain one. Examples make the intended boundary visible and help future maintainers understand why the expression exists.
Test captures and flags
Flags change how a JavaScript regex behaves. Case-insensitive matching can be useful for labels but risky for identifiers where case matters. Global matching finds multiple occurrences; multiline mode changes how ^ and $ relate to lines. A dot does not match every possible line break without an appropriate mode. Test each flag deliberately rather than copying a pattern and assuming it means the same thing in every context.
Capturing groups are useful when you need a part of the match, such as year and month from a constrained date-like string. Inspect the captures in Regex tester. A pattern may match but capture the wrong boundary because a greedy section consumed more than expected. Use noncapturing groups when a group is only for structure and give complex expressions comments outside the pattern in source code.
| Test case | Why it matters |
|---|---|
| Shortest accepted input | Checks minimum boundary |
| One character too long | Checks maximum boundary |
| Separator in wrong place | Checks structure |
| Non-ASCII text | Checks Unicode assumptions |
| Large repeated near-match | Checks performance risk |
Watch for broad and slow patterns
Patterns with nested ambiguous repetition can take unexpectedly long on near-matching input. A small test string may work instantly while a long hostile string consumes substantial CPU. Prefer precise character classes and bounded repetitions where the input format permits them. GoldieKit's tester uses a timed worker, which limits the test environment, but it cannot guarantee that the same expression is safe in every server or browser context.
If the input is structured data, use a parser. Regex is not a reliable substitute for a JSON parser, HTML parser, or full URL parser. GoldieKit has a JSON formatter and URL parser for those cases. A parser understands nesting and syntax; a pattern usually does not. For data cleaning, Find and replace can apply literal or regex replacements, but preview the affected text and keep a copy before making a broad replacement.
A worked example: find an invoice code
Suppose your internal format is INV- followed by four ASCII digits. A first pattern might be INV-\d{4}. It finds the code inside a longer string, which may be right for log search. If a form field must contain only the code, anchor it: ^INV-[0-9]{4}$. Test INV-0042, INV-42, XINV-0042, and INV-0042-extra. If lowercase inv-0042 should be accepted, decide explicitly whether to use a case-insensitive flag or normalize input before validation.
Even a full match only proves the string has the expected shape. It does not prove the invoice exists or belongs to the user. Check it against authoritative data before taking an action. This is the difference between syntactic validation and business validation.
Keep the test set with the pattern
When a regex becomes part of application code, save the accepted and rejected examples as tests. Add a long near-match to catch performance regressions. Document whether the pattern matches a whole value or searches within text, which flags are required, and which engine runs it. If requirements change, revise the examples first. A readable, tested pattern is more valuable than a clever one that only its author can explain.
Regex is not a complete input policy
After a pattern accepts text, the application may still need length limits, normalization rules, authorization checks, and safe output encoding. An invoice code can match the expected syntax while referring to another customer's invoice. A URL can match an HTTP-shaped pattern while pointing to an unwanted host. If a regex is used before a database lookup or external request, validate those separate concerns explicitly. Keep the pattern's responsibility narrow: identify a shape, extract a component, or locate text. Do not make it stand in for trust.