Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 38 ยท Performance and ReDoS

Optimizing and Hardening Regex

Practical techniques to make regular expressions fast and safe, from anchors and specific classes to atomic-group emulation, input limits, worker-thread timeouts and choosing linear-time engines.

You now know how backtracking works and how it can explode. This lesson turns that knowledge into habits: a checklist for writing fast patterns, a trick that emulates atomic groups in JavaScript, and a way to put a hard time limit around any match. The goal is two-fold. Optimizing makes normal inputs faster; hardening makes hostile inputs harmless.

Optimization checklist

  1. Anchor when you can. ^, $ and \b let the engine reject start positions immediately instead of trying each index.
  2. Be specific. [^,]* instead of .*, \d{4} instead of \d+. Specific classes cannot overshoot, so they need no backing off.
  3. Start with a literal. If every match contains ERROR, a pattern beginning with that literal lets the engine use a fast substring search to find candidates.
  4. Avoid needless captures. Use (?:...) when you do not need the group's text.
  5. Prefer simple string methods for simple jobs: includes, startsWith, indexOf, split(",") are faster and clearer than a regex.
  6. Do not recompile in loops. Build regexes once, outside the loop, or use literals, which the engine caches per site.
  7. Avoid long alternations of literals when a class or a trie-like structure works: /colou?r/ beats /color|colour/.
  8. Match once, extract once. Do not test with one regex and then run a second regex on the same string to extract; use exec or match and check the result.
Node.js Specific beats greedy-and-fix

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Emulating atomic groups

An atomic group (?>...) matches once and never gives characters back, which prunes huge parts of the search tree. PCRE, Java, Python 3.11+ and .NET support it directly, as do possessive quantifiers (a++). JavaScript has neither, but a lookahead is atomic by nature, because once it succeeds the engine will not re-enter it. Capture inside the lookahead and consume it with a backreference:

TEXT
(?>a+)   ==   (?=(a+))\1
Node.js Atomic group emulation with lookahead

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The numbered backreference (\1, \2) must match the number of the capture group inside its lookahead. In the hardened pattern, each iteration of the outer group takes a whole run of a atomically, so there is only one way to split the input and a failure is detected after one pass.

Limit the input

The cheapest defense is a length limit. A pattern that is quadratic on 100 characters is harmless; the same pattern on 1 MB is an outage. Before running a regex on untrusted text:

  • reject or truncate input beyond a sensible size (names: 100, URLs: 2,000, comments: 10,000);
  • apply cheaper checks first (type, length, character set);
  • never run user-supplied regexes on user-supplied text on your main thread.

A hard time limit with worker threads

JavaScript has no built-in regex timeout, but you can run the match in a worker thread and terminate it if it takes too long. Terminating a worker stops even a regex stuck in backtracking.

Node.js Timeout a runaway regex

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Starting a worker per match is heavy, so use a small pool in production, or run untrusted patterns in a separate service. For user-supplied patterns, also cap the pattern length and refuse constructs you do not need.

Choose a safer engine

Option What you get
RE2 (C++, bindings exist for many languages) Linear time, no backreferences or lookaround
Go regexp, Rust regex Same guarantee built in
.NET RegexOptions.NonBacktracking Linear matching with a restricted feature set
.NET or Java match timeouts A time limit on a backtracking engine
V8 experimental linear engine Behind a flag; falls back to backtracking for unsupported features
Python regex module timeout= Time limit on a backtracking engine

See regex flavors and engines for how these relate. For a web service that accepts patterns from users, a linear engine is the right default.

Review checklist for every pattern that touches untrusted input

  1. Is there a quantifier on a group containing another quantifier or an alternation?
  2. Can two adjacent parts match the same characters?
  3. Is the repetition bounded?
  4. Is the input length limited first?
  5. Did I test with a long, almost-matching input?
  6. Is there a timeout or a linear engine as a last line of defense?

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which change usually makes a pattern both faster and safer?

    A negated class cannot run past the delimiter, so the engine has no overshoot to undo.
  2. 2. How can you emulate the atomic group (?>a+) in JavaScript?

    A lookahead is atomic once it succeeds, and the backreference then consumes exactly what it captured.
  3. 3. What is the cheapest defense against ReDoS?

    Even a quadratic pattern is harmless on short input, and try/catch cannot interrupt a long-running match.
  4. 4. Why run a regex in a worker thread with a timeout?

    JavaScript cannot interrupt a regex on its own thread, but a worker can be stopped from outside.

Summary

  • Anchor, be specific, start with literals, avoid needless captures and recompilation.
  • Use string methods when a regex is overkill.
  • Emulate atomic groups in JavaScript with (?=(...))\1; use real atomic groups and possessive quantifiers where the engine has them.
  • Limit input length first; add a worker-thread timeout for risky matching.
  • Prefer a linear-time engine for untrusted patterns or input.

Next: Regex in JavaScript: The Complete API