分析 JavaScript 正则表达式的回溯风险和状态转换

POST/regex/analyze
公开访问裸值兼容

静态分析正则表达式,返回嵌套量词、可能的回溯风险和验证样本,并为支持的表达式生成 NFA/DFA 图。样本匹配与替换预览在工具页的浏览器线程中执行。 访问类别:匿名 API,可携带 Token 使用独立额度

请求体

application/json
pattern必填
string · 最长:512 字符
flags
string · 可组合 g、i、m、s、u、v、d、y,其中 u 和 v 互斥。 · 最长:8 字符

裸值请求体

可直接发送 pattern 的 JSON 值。需要传入可选字段时,使用 JSON 对象。

响应

application/json

{
  "type": "object",
  "properties": {
    "repeatedQuantifiers": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "start": {
            "type": "integer"
          },
          "end": {
            "type": "integer"
          }
        },
        "required": [
          "start",
          "end"
        ]
      }
    },
    "risk": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "status": {
              "type": "string",
              "enum": [
                "unknown"
              ]
            },
            "reason": {
              "type": "string"
            }
          },
          "required": [
            "status",
            "reason"
          ]
        },
        {
          "type": "object",
          "properties": {
            "status": {
              "type": "string",
              "enum": [
                "no-risk-found"
              ]
            },
            "complexity": {
              "type": "string",
              "enum": [
                "constant",
                "linear",
                "safe"
              ]
            }
          },
          "required": [
            "status",
            "complexity"
          ]
        },
        {
          "type": "object",
          "properties": {
            "status": {
              "type": "string",
              "enum": [
                "vulnerable"
              ]
            },
            "complexity": {
              "type": "string",
              "enum": [
                "polynomial",
                "exponential"
              ]
            },
            "degree": {
              "type": "integer"
            },
            "hotspots": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "start": {
                    "type": "integer"
                  },
                  "end": {
                    "type": "integer"
                  }
                },
                "required": [
                  "start",
                  "end"
                ]
              }
            },
            "sample": {
              "type": [
                "string",
                "null"
              ]
            }
          },
          "required": [
            "status",
            "complexity",
            "hotspots",
            "sample"
          ]
        }
      ]
    },
    "automata": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "nfa": {
          "type": "object",
          "properties": {
            "states": {
              "type": "integer"
            },
            "mermaid": {
              "type": "string"
            }
          },
          "required": [
            "states",
            "mermaid"
          ]
        },
        "dfa": {
          "type": [
            "object",
            "null"
          ],
          "properties": {
            "states": {
              "type": "integer"
            },
            "mermaid": {
              "type": "string"
            }
          },
          "required": [
            "states",
            "mermaid"
          ]
        },
        "ignoresAssertions": {
          "type": "boolean"
        }
      },
      "required": [
        "nfa",
        "dfa",
        "ignoresAssertions"
      ]
    }
  },
  "required": [
    "repeatedQuantifiers",
    "risk",
    "automata"
  ]
}

响应示例

成功响应

curl -sS --json '"^(a+)+$"' https://oit.sh/regex/analyze
HTTP 200
content-type: application/json
content-encoding: gzip
cache-control: no-store

{
  "repeatedQuantifiers": [
    {
      "start": 3,
      "end": 4
    }
  ],
  "risk": {
    "status": "vulnerable",
    "complexity": "exponential",
    "hotspots": [
      {
        "start": 2,
        "end": 3
      }
    ],
    "sample": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\u0000"
  },
  "automata": {
    "nfa": {
      "states": 2,
      "mermaid": "flowchart LR\n%% nodes\n\tnull(( ))\n\tn0((0))\n\tn1(((1)))\n\n%% edges\n\tnull -.-> n0\n\tn0 -->|\"U+0061\"| n1\n\tn1 -->|\"U+0061\"| n1"
    },
    "dfa": {
      "states": 2,
      "mermaid": "flowchart LR\n%% nodes\n\tnull(( ))\n\tn0((0))\n\tn1(((1)))\n\n%% edges\n\tnull -.-> n0\n\tn0 -->|\"U+0061\"| n1\n\tn1 -->|\"U+0061\"| n1"
    },
    "ignoresAssertions": true
  }
}

错误示例

curl -sS --json '{}' https://oit.sh/regex/analyze
HTTP 400
content-type: application/json
content-encoding: gzip
cache-control: no-store

{
  "error": {
    "code": "INVALID_INPUT",
    "message": "参数无效",
    "issues": [
      {
        "location": "body",
        "field": "pattern",
        "code": "invalid_type",
        "message": "需要 字符串 类型"
      }
    ]
  }
}

在线调试

必填
Ctrl / ⌘ + Enter
curl -X POST 'https://oit.sh/regex/analyze' \
  -H 'Content-Type: application/json' \
  --data-raw '{"pattern":"^(a+)+$"}'

响应

等待请求

等待发送请求