Home → JSON Regex Generator
Paste a JSON object and get a regular expression for every field — ready to paste into the JSON Schema pattern keyword. Below the tool: 14 tested patterns for common JSON field types, and the escaping rules that break most hand-written patterns.
The generator reads each field of a JSON object, infers what kind of value it holds, and emits an anchored regex for it. The output is ECMA 262 — the dialect JSON Schema's pattern keyword uses — so patterns paste straight into a schema or into JavaScript. Everything runs in your browser; no JSON is sent to a server. Free, no account required.
Give it one JSON object and it returns a field-by-field map of regex patterns — the tedious half of writing a JSON Schema by hand.
Paste a sample object — typically one representative API response — and each key comes back with its own pattern. A value like "user@example.com" is recognised as an email and gets the email pattern; "2024-01-01" gets an ISO 8601 date pattern; numbers get ^\\d+$ or a decimal pattern; booleans get ^(true|false)$. Recognised formats are email, phone, URL and ISO date.
When a string matches none of the known formats, the default output is a permissive length range. Tick Strict mode and you get an escaped exact-value match instead — useful for enum-like fields such as status codes or currency symbols, where the set of valid values is fixed. The generator reads top-level keys, so flatten or run nested objects separately.
For field types the generator cannot infer from a single sample — UUID, semantic version, hex colour, Base64, URL-safe slug — the pattern reference table further down this page lists tested, anchored regexes to copy by hand.
Generated patterns integrate directly into JSON Schema validation via the pattern keyword.
Add "pattern": "your-regex-here" to any string type field in your JSON Schema. The value must match the regex (using ECMA 262 dialect) for the schema validation to pass. The pattern must match the complete string, not just a substring.
Always test regex patterns against both valid examples (which should match) and invalid examples (which should not) before deploying. Edge cases like empty strings, unicode characters, and boundary values often reveal pattern issues.
Copy these tested regex patterns directly into your JSON Schema pattern keyword. All patterns use ECMA 262 regex dialect, compatible with JSON Schema validators and JavaScript.
| Field Type | Pattern | Example valid value |
|---|---|---|
| Email address | ^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$ | alice@example.com |
| UUID v4 | ^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ | 550e8400-e29b-41d4-a716-446655440000 |
| ISO 8601 date | ^\d{4}-\d{2}-\d{2}$ | 2026-03-21 |
| ISO 8601 datetime | ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+\-]\d{2}:\d{2})$ | 2026-03-21T14:30:00Z |
| URL (http/https) | ^https?:\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-@?^=%&/~\+#])?$ | https://example.com/path |
| IPv4 address | ^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$ | 192.168.1.1 |
| Hex color code | ^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$ | #FF5733 |
| Semantic version | ^\d+\.\d+\.\d+$ | 1.2.34 |
| Slug (URL-safe) | ^[a-z0-9]+(?:-[a-z0-9]+)*$ | my-product-name |
| Phone (E.164) | ^\+[1-9]\d{1,14}$ | +14155552671 |
| Credit card (length only — no Luhn check) | ^\d{13,19}$ | 4111111111111111 |
| Base64 encoded | ^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$ | SGVsbG8gV29ybGQ= |
Here is a real-world JSON Schema that uses pattern to validate multiple fields in a user registration object. Copy and adapt it for your own API.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "User Registration",
"type": "object",
"required": [
"email",
"username",
"birthDate"
],
"properties": {
"email": {
"type": "string",
"pattern": "^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$",
"description": "Valid email address"
},
"username": {
"type": "string",
"pattern": "^[a-z0-9_]{3,20}$",
"description": "3-20 lowercase alphanumeric characters or underscores"
},
"birthDate": {
"type": "string",
"pattern": "^\\d{4}-\\d{2}-\\d{2}$",
"description": "ISO 8601 date (YYYY-MM-DD)"
},
"website": {
"type": "string",
"pattern": "^https?://.+",
"description": "Optional URL starting with http or https"
},
"phoneNumber": {
"type": "string",
"pattern": "^\\+[1-9]\\d{1,14}$",
"description": "E.164 international phone number"
}
}
}
pattern keyword uses ECMA 262 regex (same as JavaScript). Avoid PCRE-only features like lookbehind assertions.\d{4} will match "hello1234world". Always anchor with ^ and $ to require a full match.\\d in JSON to represent the regex \d.pattern keyword only applies to values of type string. It is ignored for other types.JSON has no regular expression type. Its six value types are string, number, object, array, boolean and null, so a regex can only ever live in a JSON document as an ordinary string. Nothing in the JSON specification will match it against anything.
Matching happens one layer up, and there are only two places it can happen:
pattern, patternProperties and propertyNames keywords. The validator applies the regex for you at validation time.JSON.parse then test the field, or query with a path expression first. Running a regex over the raw JSON text instead is the classic mistake: whitespace, key order, unicode escapes and nesting all vary without changing the data, so text-level patterns break on valid input.So a search for “JSON regex” almost always resolves to one of these two, and in practice usually the first.
The pattern keyword only ever looks at a string value. When the key names themselves are dynamic — feature flags, locale codes, tenant IDs, metric names — you need two different keywords.
Applies a subschema to every property whose name matches the regex. Here any key shaped like a locale code must hold a non-empty string, and additionalProperties: false rejects keys that match nothing:
{
"type": "object",
"patternProperties": {
"^[a-z]{2}(-[A-Z]{2})?$": { "type": "string", "minLength": 1 }
},
"additionalProperties": false
}
That accepts {"en": "Hello", "en-GB": "Hello"} and rejects {"english": "Hello"}. Note that patternProperties regexes are deliberately unanchored in the same way as pattern, and a key matching two of them must satisfy both subschemas.
Constrains the key names without saying anything about the values — useful for enforcing a naming convention across a whole object:
{
"type": "object",
"propertyNames": { "pattern": "^[a-z][a-zA-Z0-9]*$" }
}
Every key is validated as if it were a string against that subschema, so this object rejects snake_case and PascalCase keys while allowing camelCase.
Four failure modes account for nearly every “my pattern does not work” report, and none of them raise an error — the validator simply passes data it should have rejected.
| Symptom | Cause | Fix |
|---|---|---|
| Pattern matches nothing at all | Single-escaped backslash. In a JSON file "\d" is an invalid escape, and lenient parsers collapse it to the literal letter d. | Write "\\d". Double every backslash. |
| Junk values pass validation | Unanchored pattern. JSON Schema tests for a substring, so \\d{4} accepts "hello1234world". | Wrap with ^ and $. |
| Pattern ignored entirely | Applied to a non-string. pattern is a no-op on numbers, booleans and null. | Add "type": "string", or use minimum/maximum for numbers. |
| Works locally, fails in CI | PCRE-only syntax. Lookbehind, atomic groups and inline flags such as (?i) are outside ECMA 262. | Stay within ECMA 262; spell out character classes instead of using flags. |
Once a pattern is in place, run real payloads through the JSON Schema validator — or a whole directory of them through the batch validator — to confirm it rejects what it should.
Explore more tools: All JSON Tools | Validator | Pretty Print | JSON Diff