Code & Engineering

ReDoS Regex Rewrite: Same Answers, Linear Time

Rewrites a JavaScript regular expression that can hang on a crafted input (catastrophic backtracking, ReDoS) into one that gives exactly the same yes or no for every text and runs in linear time, answered as JSON with the pattern and flags. Knows the shapes that explode - a quantified group whose inside is quantified too, alternatives under a star that can match the same text, an optional separator between repeats, a counted group of runs, an unanchored run before a literal - and how to remove the ambiguity without atomic groups, which JavaScript lacks. Keeps the flags, keeps an already linear pattern as it is, and answers with a CANNOT line when no regular expression can give the same answers, such as a backreference that compares two arbitrary substrings. Use when a regex in a validator or a search times out, when a security scan reports ReDoS or catastrophic backtracking, or when asked to make a regex safe for untrusted input.

ReDoS Regex Rewrite: Same Answers, Linear Time is a SKILL.md that rewrites a JavaScript regular expression that can hang on a crafted input (catastrophic backtracking, ReDoS) into one that gives exactly the same yes or no for every text and runs in linear time, answered as JSON with the pattern and flags; an agent buys it once for $0.02 over x402.

Tested 2026-10-09No code, no hidden instructionsv1.0.0 · 8.9 KB · perpetual license

Not for

Writing a new regular expression from examples, changing what a pattern accepts, other regex engines than the JavaScript one in Node and the browsers, or proving a pattern safe for every engine. It rewrites one pattern used as a yes or no test and keeps its flags; matched text and capture groups are not preserved unless a backreference needs them.

Tested, honestly

Tested 2026-10-09 with a strong and a weak model.

With and without the skill

Results with and without the skill, for Sonnet and Haiku
SonnetHaiku
withwithoutwithwithout
Patterns handled right (22 patterns)22/2221/2222/2221/22

Same request on both sides; it states the JSON shape and asks for a CANNOT line when no linear pattern gives the same answers. Without the skill both models already rewrote all thirteen hanging patterns correctly and kept the safe ones. Each missed one refusal: for a pattern whose first word must equal its last word they returned another pattern with the same backreference, which still is not linear.

Same cases and the same checks with and without the skill. The cases are ours, written around what the skill is for; with a handful of cases, a difference of one or two is within noise.

SonnetStrong model, claude-sonnet-5-5
Right on all 22, checked by running every answer: the same yes or no as the original on a pool of short texts, and each near-miss input finished within 200 ms. It collapsed nested runs, made optional separators mandatory where the set allows, shrank unanchored runs to one character, worked out a glued repeat of digits and decimals, turned a backreference over runs of one letter into an even count, kept the flags, left seven linear patterns as they were or rewrote them to the same set, and refused both patterns whose backreference repeats arbitrary text.
HaikuWeak model, claude-haiku-5-5
Right on all 22, checked by running every answer, with the same rewrites, the same seven safe patterns kept and the same two refusals as Sonnet.

Full test summary

Example

Our own test text, before and after the skill ran. Excerpts only.

English · claude-sonnet-5-5

Before

Pattern (source, no slashes): ^(a+)+$ Flags: (none)

After

{"pattern": "^a+$", "flags": ""}

What is in the file

  • The answer
  • Only the yes or no counts
  • The shapes that hang
  • How to rewrite
  • When the pattern is already safe
  • When to refuse
  • Work in this order
  • Short example

Languages

English. Tried in: English.

License

Perpetual, non-exclusive; use and modify for yourself incl. paid work; no resale or republishing. Holder: Georgi Kalchev, aiskills402.com. Full terms.

Versions

Current version 1.0.0, updated 2026-10-09. Whoever bought an earlier version gets new ones free through the same re-download token.

  1. v1.0.0 · 2026-10-09

    First release: rewrites a JavaScript regular expression used with test() that can backtrack catastrophically into one that accepts exactly the same texts in linear time, answered as JSON with the pattern and the unchanged flags; returns an already linear pattern unchanged and answers with a CANNOT REWRITE line when a backreference must repeat arbitrary text. The examples in the skill are not the test patterns. The tests measure the accepted set of each original over a pool of short strings, run every answer on near-miss inputs under a 200 ms limit, and the case script proves each trap really hangs and each control does not. No model run yet: the baseline, the final price and the sentence on what Sonnet gains are still to be written. Price 40000 is provisional.

FAQ

How can a short pattern hang a whole server?

The engine in Node and the browsers backtracks: when a match fails at the end, it goes back and tries every other way the pattern could have split the text. A group of runs inside another run, two alternatives that start alike, or an optional separator between repeats gives a forty-character string billions of splits. The input that triggers it is usually a near miss, such as letters followed by one wrong character.

Why not just add a lazy quantifier or an atomic group?

JavaScript has no atomic groups and no possessive quantifiers, so those patterns do not compile. A lazy quantifier only changes the order in which the splits are tried, not how many there are, so the near miss still hangs. The fix is a pattern in which every character can be consumed in one way only, written from the set of texts the original accepts, with its edges checked.

When does it refuse to rewrite?

When a backreference has to repeat an arbitrary earlier piece of text, such as the same word twice or the same number on both sides of a dash, no regular expression can describe that set, so no rewrite can give the same answers quickly. You get one CANNOT REWRITE line with the reason. A request to change what the pattern accepts is refused too, because that is a new regex, not a safe copy.

Does it help Claude Sonnet?

Only a little, so two cents. Both Claude models rewrote twenty-two patterns, file loaded and not, and every answer was run: same yes or no as the original on thousands of short texts, and no near-miss input above 200 ms. Plain Sonnet already fixed all thirteen hanging patterns. It missed one refusal: a pattern whose first word must equal its last got another backreference pattern back, still not linear. With the file it refused it. Haiku also went from 21 to 22.

Read more

  • ReDoS: the regex that locks your server

    A single pattern in a validator can pin a Node process when the input is crafted to fail. Here is why it happens, how to spot it, and how to fix it.

    28 Oct 2026 · 7 min read

Share

Read this page as Markdown: /skills/redos-regex-rewrite.md.

  • Regex From Examples for Any Script

    Code & Engineering

    SKILL.md · v1.0.0 · 6.2 KB

    Writes a JavaScript regular expression from a description plus lists of strings that must match and must not match, answered as JSON with the pattern and flags. Knows the traps measured in Node 24 - \b and \w are ASCII-only even with the u flag, so \bдума\b never matches Cyrillic text and \w+ cuts "café" to "caf"; a word boundary for any script is a pair of lookarounds with \p{L} and the u flag. Anchors a validator so extra text around the value is rejected, adds the s or m flag when the input spans lines, and avoids nested quantifiers such as (\w+\s?)* that hang on a near-miss string. Use when asked to write, fix or tighten a regex, a validation pattern or a text-search pattern, especially for non-English text.

    $0.01once

    • x402
    • USDC
    • Base
    Get skill

    Tested with Sonnet and Haiku, 8 Oct 2026

  • Code Review

    Code & Engineering

    SKILL.md · v1.0.4 · 6.0 KB

    Reviews a code change (a diff or a changed file pasted as text) and reports real problems ranked by severity — correctness bugs, security holes, data loss, missing error handling, edge cases, resource leaks — each with location, reason and a concrete fix in words. Use when asked to review code, a diff or a pull request, check a change for bugs, or find security problems in a snippet.

    $0.01once

    • x402
    • USDC
    • Base
    Get skill

    Tested with Sonnet and Haiku, 30 Sep 2026

  • Cloudflare Workers Review: D1, OpenNext, Fetch

    Code & Engineering

    SKILL.md · v1.2.0 · 9.8 KB

    Reviews pasted Cloudflare Workers code and configuration (a Worker, a Next.js app on OpenNext, D1 queries, wrangler config) for platform pitfalls that pass every local test and then fail in production or quietly cost money. It flags the edge runtime under OpenNext, a fetch to your own Worker's workers.dev address (error 1042), D1 queries that bind more than 100 parameters as data grows, LIKE patterns over D1's 50-byte limit, reading rowsAffected where D1 returns meta.changes, interactive transactions D1 does not have, secrets kept in plain vars, outbound fetch code that treats only a thrown error as failure, a browser User-Agent that bot protection challenges, client hop-by-hop headers forwarded to fetch, cron triggers whose day of the week is written as numbers, and unbounded queries on the request path. Each finding has a fixed code, the place, the reason and a fix, then one verdict. Use to review a Cloudflare Worker before deploy, check D1 or wrangler code, or audit a Next.js app running on Cloudflare.

    $0.05once

    • x402
    • USDC
    • Base
    Get skill

    Tested with Sonnet and Haiku, 8 Oct 2026

More in Code & Engineering

  • Diff From Before and After: A Patch That Applies Exactly

    Code & Engineering

    SKILL.md · v1.0.0 · 9.4 KB

    Writes the unified diff (the patch format of git diff and diff -u) that turns an old version of a text file into a new one, so that git apply or patch applies it to the old version with no fuzz and produces the new version byte for byte. Compares line by line with every space, tab, carriage return and final line break counting, writes each hunk with three lines of context, merges hunks whose context touches, counts the numbers in every hunk header from the hunk's own lines, adds the backslash line for a missing final line break on the right side, and handles new files, emptied files, repeated lines and lines that begin with dashes or plus signs. Answers with the diff only, no code fence. Two identical versions get a fixed one-line NO CHANGES answer. Use when asked to make a patch, produce or generate a diff, show what changed, or turn a before and after into something a patch program accepts.

    $0.01once

    • x402
    • USDC
    • Base
    Get skill

    Tested with Sonnet and Haiku, 8 Oct 2026

  • Playwright Test from Manual Steps

    Code & Engineering

    SKILL.md · v1.0.0 · 10.9 KB

    Turns manual test steps plus the page facts you have (route, labels, button names, messages) into one Playwright Test file in TypeScript that can really fail. Elements are found by role and accessible name, by label or by text, never by a CSS class, an id or XPath, and a name that is the start of another gets the exact option; every step that observes something gets one awaited, auto-retrying assertion; a step such as wait a couple of seconds becomes an assertion on the state it waits for, never a fixed pause; the address is relative because the base URL lives in the config; a third-party widget is mocked; a dialog handler is registered before the click that opens it; expected text is copied byte for byte; a control the steps do not name is marked with a fixme and a comment instead of a guessed locator. Use to write a Playwright test from manual test steps, convert a QA test case or acceptance criteria into an end-to-end test, replace waitForTimeout in a flaky test, or make a recorded test use role locators.

    $0.02once

    • x402
    • USDC
    • Base
    Get skill

    Tested with Sonnet and Haiku, 8 Oct 2026

  • SQL Dump to SQLite: MySQL and Postgres Rows, Unchanged

    Code & Engineering

    SKILL.md · v1.0.0 · 12.4 KB

    Converts a MySQL or PostgreSQL dump (CREATE TABLE, INSERT rows, COPY blocks) into SQL that SQLite, Cloudflare D1 and Turso execute, with every value in every row unchanged, and answers with the SQL only, no code fence. Decodes string escapes the right way for each source (MySQL backslash sequences, Postgres E strings and dollar quotes, COPY escapes with its null marker), keeps strings that look like numbers as strings, turns booleans into 0 and 1, writes hex values as real blobs, keeps dates as the text the dump has, maps enum to text with a CHECK, maps auto-increment keys, drops what SQLite does not have (engine, charset, session settings, locks, begin and commit, schema prefixes), orders tables and rows so foreign keys hold, and splits long inserts. A type with no exact equivalent, a COPY row with the wrong number of fields and a paste cut in the middle get a one-line CANNOT CONVERT answer naming the place, instead of a guess. Use when asked to move a MySQL or Postgres dump into SQLite, D1 or Turso, to load a dump file, or to fix a dump that fails there.

    $0.03once

    • x402
    • USDC
    • Base
    Get skill

    Tested with Sonnet and Haiku, 8 Oct 2026

  • Unified Diff Repair: Fix the Counts, Keep the Change

    Code & Engineering

    SKILL.md · v1.0.0 · 10.5 KB

    Repairs a unified diff (the patch format of git diff and diff -u) so that git apply and patch accept it against the file it was written for, and answers with the diff only, no code fence. Recounts the numbers in each hunk header from the hunk's own lines, puts back context lines and removed lines exactly as the file has them (tabs, trailing spaces, blank lines, carriage returns), adds the backslash line that marks a missing final line break, restores hunk order, merges overlapping hunks and puts back an unchanged line that a hunk lost. Every added and removed line stays exactly as written, and no change is improved or re-indented. When the lines a hunk expects are not in the file, or fit in more than one place, a fixed one-line CANNOT REPAIR answer replaces a guessed patch. Use when an agent, a tool or another model produced a patch that is rejected, reports a corrupt patch, or looks wrong, or when asked to fix, recount or make a diff apply.

    $0.02once

    • x402
    • USDC
    • Base
    Get skill

    Tested with Sonnet and Haiku, 8 Oct 2026