JSON & Schema

JSON Schema Pattern

The pattern keyword applies a regular expression to a string instance. Test the regex first, then escape it correctly inside JSON and validate representative values.

Published

Cross-tool workflow

Test Regex → Escape for JSON → Validate Schema

Three functional steps close one task; none of them is a promotional detour.

  1. Test Regex

    Step 1 · Real Regex Tester

    Test the pattern before the schema

    Use the regex tester to inspect matching behavior before JSON escaping is added.

    ^[A-Z]{2}-\d{4}$

    Expected result: XY-9876 matches; DEV-2048, dev-2048 and prefix DEV-2048 do not.

  2. Escape for JSON
    ^[A-Z]{2}-\d{4}$
    "pattern": "^[A-Z]{2}-\\d{4}$"

    The regex contains \d; the JSON string represents that backslash as \\.

  3. Validate Schema

    Step 3 · Real Schema Validator

    Validate an anchored identifier

    The schema accepts only two uppercase letters, a hyphen and four digits.

    { "type": "string", "pattern": "^[A-Z]{2}-\\d{4}$" }

    Expected result: XY-9876 is valid; DEV-2048 and dev-2048 are invalid.

Why this fails

Four checks before shipping a pattern

Missing anchors

Unanchored patterns can match inside a larger string.

Wrong JSON escaping

A regex backslash must survive JSON string parsing.

Only positive cases

Always test strings that must fail as well.

Engine assumptions

Keep syntax portable when schemas move between validators.

Practice with the full tool

Validate your production pattern

Take the regex you tested here and validate it inside your real JSON Schema.

Continue in JSON Schema Validator →