consolelog.tools

Regex Test Case Generator

Generate comprehensive test cases for regular expressions

About this ToolHow it works, benefits & use cases

The Regex Test Case Generator inspects a regular expression and writes a runnable test suite around it for Jest, Vitest, or Mocha with Chai. Paste a pattern with or without delimiters — both /^[a-z]+$/gi and bare ^[a-z]+$ are accepted — and the generator recognises the kind of pattern (email, URL, phone number, digit-only, or generic) to produce sensible positive and negative cases. The output is split into "Should match" and "Should not match" describe blocks, each calling regex.test() and asserting true or false, with string inputs safely escaped. Turn on "Include Edge Cases" to add empty-string, special-character, and whitespace-only assertions. Common-pattern buttons preload tried-and-tested expressions for email, URL, phone, hex colour, and IP address so you can generate a suite without writing a regex yourself. Copy the generated test file or download it with a framework-appropriate name, then refine the cases for your exact pattern.

How to Use

  1. 1Select a Test Framework (Jest, Vitest, or Mocha + Chai) and toggle "Include Edge Cases" if you want them.
  2. 2Enter your regex in the pattern field — with delimiters and flags (/pattern/flags) or as a bare pattern.
  3. 3Or click a Common Patterns button (Email, URL, Phone, Hex Color, IP Address) to load a ready-made expression.
  4. 4Click "Generate Test Cases" to build the Should-match and Should-not-match describe blocks.
  5. 5Copy the generated suite or download it as a .test.js / .spec.js file and adjust cases for your pattern.

Key Benefits

  • Accepts patterns with or without /delimiters/ and flags
  • Detects email, URL, phone, and digit patterns to generate relevant inputs
  • Splits output into "Should match" and "Should not match" describe blocks
  • Optional edge cases for empty strings, special characters, and whitespace-only input
  • Common-pattern presets for email, URL, phone, hex colour, and IP address
  • String inputs are escaped so generated test literals stay valid
  • Framework-aware import line and download file name for Jest, Vitest, or Mocha

Common Use Cases

  • Generating a baseline test suite for a validation regex before refining it
  • Documenting which inputs a pattern should and should not match
  • Producing edge-case assertions to harden a fragile expression
  • Creating a starting suite for a common pattern like email or IP from the presets
  • Switching an existing regex suite between Jest, Vitest, and Mocha syntax

Options

Common Patterns:

Enter pattern with or without delimiters (e.g., /pattern/flags or pattern)

Jest test suite

Generated test cases will appear here...

Was this tool helpful?

Share Your Experience

Help others discover this tool!

Related tools