← Back to all regex patterns
Validation
Catastrophic backtracking risk
Regex with Nested Quantifiers
This is a teaching pattern for catastrophic backtracking risk. It looks simple, but nested quantifiers can turn certain non-matching inputs into expensive regex evaluations.
Regex
/^(a+)+$/Try this pattern
Matches current input
aaaa
1 match found in the current text.
Passing examples
- aaaa
Simple repeated input matches.
Failing examples
- aaaaaaaaaaaaaaaa!
A trailing non-match is where backtracking cost spikes.
- b
- (empty string)
Code examples
JavaScript
const regex = /^(a+)+$/g;
regex.test(input);Python
import re
pattern = re.compile(r"^(a+)+$")
bool(pattern.search(input))Go
re := regexp.MustCompile("^(a+)+$")
matched := re.MatchString(input)Related Validation Patterns
Regex for Valid Email Address
/^[^\s@]+@[^\s@]+\.[^\s@]+$/i
Regex for URL Slug
/^[a-z0-9]+(?:-[a-z0-9]+)*$/
Regex for Strong Password
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z\d]).{12,}$/
Regex for UUID v4
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
Regex for IPv4 Address
/^(?:25[0-5]|2[0-4]\d|1?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|1?\d?\d)){3}$/
Regex for Hex Color
/^#?(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/