Master regular expressions for JSON validation. Learn patterns for emails, URLs, phone numbers, and custom validation rules.
Regular expressions add powerful pattern matching to JSON validation. Whether validating email formats in user data, checking URL structures, or enforcing custom business rules, regex patterns ensure data integrity at the schema level.
The pattern keyword uses regex for string validation:
{ "type": "object", "properties": { "email": { "type": "string", "pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$" } } }
Basic Pattern: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$
RFC 5322 Compliant (Simplified):
^(?:[a-z0-9!#$%&'+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])")@(?:(?:a-z0-9?.)+a-z0-9?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-][a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])+)])$
JSON Schema Example: { "user": { "email": "[email protected]", "pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$" } }
HTTP/HTTPS URLs: ^https?://(?:a-zA-Z0-9?.)+[a-zA-Z0-9][a-zA-Z0-9-]{0,61}a-zA-Z0-9?(?:/.*)?$
Generic URI: ^[a-zA-Z][a-zA-Z0-9+.-]*://[\S]+$
UUID v4: ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$
Any UUID Version: ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$
E.164 Format: ^+[1-9]\d{1,14}$
US Phone Numbers: ^(?:(?:+?1\s*(?:[.-]\s*)?)?(?:(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x.?|ext.?|extension)\s*(\d+))?$
Basic Pattern (Format Only): ^\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}$
Specific Card Types:
IPv4: ^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) $
IPv6: ^(?:(?:[a-fA-F\d]{1,4}:){7}(?:[a-fA-F\d]{1,4}|:)|(?:[a-fA-F\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|:[a-fA-F\d]{1,4}|:)|...)$
US ZIP: ^\d{5}(?:[-\s]\d{4})?$
UK Postcode: ^(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y]0-9|[0-9][A-HJKSTUW]) [0-9][ABD-HJLNP-UW-Z]{2})$
{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "id": { "type": "string", "pattern": "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$" }, "email": { "type": "string", "format": "email", "pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$" }, "website": { "type": "string", "pattern": "^https?://[\S]+$" }, "phone": { "type": "string", "pattern": "^+[1-9]\d{1,14}$" }, "zipCode": { "type": "string", "pattern": "^\d{5}(?:[-\s]\d{4})?$" } }, "required": ["id", "email"] }
Use our regex tester tool to validate your patterns:
1. Avoid Nested Quantifiers Bad: (a+)+ - can cause catastrophic backtracking Good: Use possessive quantifiers or atomic groups
2. Use Anchors Always use ^ and $ to prevent partial matches
3. Prefer Character Classes [0-9] is faster than (0|1|2|3|4|5|6|7|8|9)
4. Limit Repetition {1,1000} is safer than {1,}
Node.js with AJV: const Ajv = require('ajv'); const ajv = new Ajv();
const schema = { type: "object", properties: { email: { type: "string", pattern: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$" } } };
const validate = ajv.compile(schema); const valid = validate({ email: "[email protected]" });
Client-Side: const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/; const isValid = emailRegex.test(userData.email);
Regular expressions combined with JSON Schema provide robust validation for any data format. Start with simple patterns and build complexity as needed. Test thoroughly with edge cases, and remember that regex is powerful but not always the answer - sometimes built-in format validators (date, email, uri) are more reliable.