Categories

Regex Linter

Detect error-prone and risky regex patterns, check for catastrophic backtracking, unanchored patterns, and provide rewrite suggestions

Regex Linter

Overview

Regex Linter analyzes regular expressions for common anti-patterns, performance issues, and correctness problems. It helps you write better, safer, and more efficient regular expressions.

Issues Detected

Critical Issues (Performance)

  • Catastrophic Backtracking: Nested quantifiers like (a+)+ that can cause exponential time complexity
  • Quadratic Behavior: Patterns like (.*)* that exhibit O(n²) performance
  • Unbounded Repeats: Greedy quantifiers without maximum length limits

Error Issues (Correctness)

  • Unescaped Dots: Using . when you mean . (literal dot)
  • Missing Anchors: Patterns that can match anywhere in the text
  • Invalid Character Classes: Ranges like [a-Z] that include unexpected characters
  • Octal Escapes: Ambiguous escapes in modern regex

Warning Issues (Readability)

  • Observable Non-Greedy: .*? patterns that could use negated character classes
  • Redundant Escapes: Unnecessary backslashes like \-
  • Double Negation: Confusing [^[^]] patterns
  • Ambiguous Alternation: a|ab where order matters

Info Issues (Style)

  • Non-Capturing Groups: Suggesting (?:...) when groups aren't referenced
  • Verbose Patterns: Can simplify ddd to d{3}

Check Levels

  • basic: Only critical performance issues
  • standard: Critical + errors + warnings (recommended)
  • strict: All issues including style suggestions

Dialect Support

  • JavaScript: ES2018+ features including lookbehind and Unicode property escapes
  • Python: Recommends raw strings, supports verbose regex option
  • PCRE: Warns about backtracking verbs if used unintentionally
  • Go (RE2): Warns about unsupported features (lookbehind, backreferences)
  • Java: Version-specific feature warnings

Example

Input:

(.*+)a

Output:

{
  "summary": {
    "score": 50,
    "severity": "error",
    "issuesFound": 2
  },
  "issues": [
    {
      "severity": "critical",
      "message": "Catastrophic backtracking risk",
      "suggestion": {
        "pattern": "[^a]*a",
        "explanation": "Use negated character class instead"
      }
    }
  ]
}

Key Facts

Category
Development
Input Types
text, select, checkbox
Output Type
json
Sample Coverage
4
API Ready
Yes

Overview

The Regex Linter analyzes regular expressions for common anti-patterns, performance issues, and correctness problems. It helps you write safer, more efficient regex by detecting issues like catastrophic backtracking and providing rewrite suggestions.

When to Use

  • When developing or debugging regular expressions to ensure they are efficient and error-free.
  • Before deploying regex in production to avoid performance bottlenecks and security risks.
  • When learning regex to understand best practices and avoid common pitfalls.

How It Works

  • Enter your regex pattern and optional flags in the input fields.
  • Select the target dialect (e.g., JavaScript, Python) and check level (basic, standard, strict).
  • The tool scans the pattern for issues such as catastrophic backtracking, unescaped characters, and missing anchors.
  • Receive a detailed report with severity levels, explanations, and rewrite suggestions if enabled.

Use Cases

Optimizing regex patterns in web applications to improve response times.
Validating user input patterns in form fields to prevent injection attacks.
Reviewing legacy code for regex performance issues during refactoring.

Examples

1. Detecting Catastrophic Backtracking

Background
A developer is writing a regex to match strings ending with 'a'.
Problem
The pattern `(.*+)a` uses a possessive quantifier that can cause catastrophic backtracking.
How to Use
Enter the regex pattern `(.*+)a` in the input field, select JavaScript as the dialect, and set check level to standard.
Outcome
The tool flags the pattern as critical for catastrophic backtracking and suggests using a negated character class like `[^a]*a`.

2. Fixing Unescaped Dots

Background
A user is writing a regex to match file paths with literal dots.
Problem
Using `.` instead of `\.` for literal dots can lead to incorrect matches across any character.
How to Use
Input the pattern, set target dialect to PCRE, and check level to standard to catch unescaped characters.
Outcome
The tool detects unescaped dots and suggests escaping them as `\.` for accurate literal matching.

Try with Samples

regex

Related Hubs

FAQ

What is catastrophic backtracking?

It's a situation where nested quantifiers in a regex can cause exponential time complexity, leading to performance issues or denial-of-service.

Which regex dialects are supported?

The tool supports JavaScript, Python, PCRE, Go (RE2), and Java, with dialect-specific warnings.

What does the check level affect?

Basic checks only critical performance issues, standard includes errors and warnings, and strict adds style suggestions.

Can I get suggestions for fixing issues?

Yes, enable the 'Include Rewrite Suggestions' option to receive optimized pattern alternatives.

Is this tool suitable for beginners?

Absolutely, it helps identify common mistakes and learn best practices in regex writing.

API Documentation

Request Endpoint

POST /en/api/tools/regex-linter

Request Parameters

Parameter Name Type Required Description
regexPattern text Yes -
regexFlags text No -
targetDialect select No -
checkLevel select No -
includeSuggestions checkbox No -

Response Format

{
  "key": {...},
  "metadata": {
    "key": "value"
  },
  "error": "Error message (optional)",
  "message": "Notification message (optional)"
}
JSON Data: JSON Data

AI MCP Documentation

Add this tool to your MCP server configuration:

{
  "mcpServers": {
    "elysiatools-regex-linter": {
      "name": "regex-linter",
      "description": "Detect error-prone and risky regex patterns, check for catastrophic backtracking, unanchored patterns, and provide rewrite suggestions",
      "baseUrl": "https://elysiatools.com/mcp/sse?toolId=regex-linter",
      "command": "",
      "args": [],
      "env": {},
      "isActive": true,
      "type": "sse"
    }
  }
}

You can chain multiple tools, e.g.: `https://elysiatools.com/mcp/sse?toolId=png-to-webp,jpg-to-webp,gif-to-webp`, max 20 tools.

If you encounter any issues, please contact us at [email protected]