ASCII to Regex Generator

Takes one or more literal ASCII sample strings, one per line, strictly validates each is 7-bit ASCII, escapes any regex metacharacters in each one, and builds a regular expression that matches exactly that set of values via literal alternation, e.g. cat, dog becomes "^(cat|dog)$". A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-27

Overview

Introduction

This tool turns a plain list of ASCII literal strings into a regular expression that matches exactly those values and nothing else.

Enter one string per line, and it builds an anchored alternation pattern like "^(cat|dog)$" you can drop straight into validation code, after strictly confirming every line is 7-bit ASCII.

What Is ASCII to Regex Generator?

A generator that converts a set of literal ASCII strings into a regex using literal alternation: each distinct value becomes one branch of a "|"-separated group, wrapped in "^(...)$" anchors.

It mirrors Integer to Regex Generator's exact approach, generalized from integers to arbitrary ASCII text, which means it also needs to escape regex metacharacters that integers never contain.

How ASCII to Regex Generator Works

Each non-blank input line is strictly validated as 7-bit ASCII using this category's shared validator; the first line containing a character above 127 stops the whole operation with that character's position and code point.

Duplicate lines are removed while preserving first-seen order, then each remaining literal has its regex metacharacters (., *, +, ?, ^, $, {, }, (, ), |, [, ], \) escaped with a backslash.

The escaped literals are joined with "|" and wrapped as "^(...)$" so the pattern matches a full string equal to exactly one of the listed literals.

When To Use ASCII to Regex Generator

Use it when you need to validate that user input is one of a small, fixed set of allowed ASCII strings, like status codes, command names, or category slugs.

It's also useful for quickly turning a spreadsheet column of allowed ASCII values into a regex for a config file or validation library, with the ASCII check catching any stray non-ASCII entry.

Features

Advantages

  • Guarantees the resulting regex matches exactly the listed set, no more and no fewer values.
  • Correctly escapes regex metacharacters in each literal, unlike a naive string-join that would leave a stray . or * to accidentally act as a wildcard.
  • Deduplicates repeated input lines automatically.

Limitations

  • Not optimized for common substrings: a set of similar strings produces one full alternation branch per value rather than a compact factored pattern.
  • Capped at 500 input lines to keep the generated pattern a reasonable size.
  • Rejects the entire input if even one line contains a non-ASCII character, rather than skipping just that line.

Examples

Three literal strings

Input

cat
dog
bird

Output

^(cat|dog|bird)$

The three strings are deduplicated (none were duplicates here), then joined with "|" inside anchored parentheses.

Literals containing regex metacharacters

Input

a.b
c*d

Output

^(a\.b|c\*d)$

The literal period and asterisk are each escaped with a backslash so they match literally instead of acting as regex wildcards.

Best Practices & Notes

Best Practices

  • Keep the input list to the exact set you want to allow; anything not listed will not match.
  • Remove the ^ and $ anchors if you need the pattern to match a value as a substring within a longer string.

Developer Notes

Input lines are split on "\n", trailing "\r" stripped, blank lines dropped, and each remaining line validated via the category's shared validateStrictAscii helper before deduplication via a Set (preserving insertion order) and metacharacter escaping with input.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), then joined with "|" and wrapped in "^(...)$", mirroring integer-to-regex-generator.ts's structure exactly aside from the ASCII validation and escaping steps integers don't need.

ASCII to Regex Generator Use Cases

  • Validating that a form field's value is one of a small fixed set of allowed ASCII codes or names
  • Turning a spreadsheet column of allowed string values into a config-file regex
  • Quickly building a test regex fixture for a known set of ASCII string values

Common Mistakes

  • Expecting the tool to factor out common prefixes or suffixes into a more compact pattern; it lists every value individually.
  • Including a non-ASCII line and being surprised the whole operation fails, rather than just that one line being skipped.
  • Forgetting the anchors mean the pattern requires an exact full-string match, not a substring match.

Tips

  • Pair this with Regex to ASCII Generator to double-check the pattern by sampling matches from it.
  • If a literal contains characters like . or *, verify the output escapes them, don't assume a hand-written alternation would have remembered to.

References

Frequently Asked Questions