为什么正则表达式模式会导致;“坏字符串”;jsonschema中的错误?

为什么正则表达式模式会导致;“坏字符串”;jsonschema中的错误?,json,regex,schema,jsonschema,Json,Regex,Schema,Jsonschema,我使用正则表达式在json模式中验证属性 模式如下: { "$schema": "http://json-schema.org/draft-07/schema", "$id": "http://example.com/example.json", "type": "object", "required": [

我使用正则表达式在json模式中验证属性

模式如下:

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "required": [
        "library_version"
    ],
    "properties": {
        "library_version": {
            "$id": "#/properties/library_version",
            "type": "string",
            "title": "Library version",
            "description": "The library version (e.g. 0.0.1) used to create this file.",
            "pattern": "^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$",
            "message": {
                "required": "Library version is a required property",
                "pattern": "The version must be a semantic form, like 0.0.1 (see https://semver.org/)"
            },
            "examples": [
                "0.0.1"
            ]
        }
    }
}
这里的大正则表达式是一个

使用我得到:


我当然不必在json模式中转义正则表达式模式?字符串应该被视为文本,对吗?有人知道这里到底发生了什么吗?

如果不转义,就不能使用某些字符,即使是在文字字符串中:


您的正则表达式需要转义所有反斜杠(您可以使用链接中的工具进行转义)。

ermahgold。因为正则表达式还不够疯狂。不过,这是一个有用的转义工具。谢谢你,奥利!这是JSON和Javascript字符串的要求,而不是JSON模式的任何特定要求。如果使用Javascript创建模式,则可以在正则表达式上使用
toString
方法:
Error: Parse error on line 14:
...le.",            "pattern": "^(0|[1-9]\d*)\.(0|[
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'