Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

schema - Escaping Regex to get Valid JSON

In my schema, I want to recognize certain patterns to restrict the type of data that a user can enter. I use regex to restrict what a user can enter, but the regex get flagged when I try to validate the JSON using an online validator like this one.

Is there a way to make the validator ignore the regex special chars that are disagreeing with it, but still keep the regex?

The weird thing is that the validator only trips up on certain instances. For instance, it flags the second and not the first instance of regex despite them being identical here:

            "institutionname": {
                "type": "string",
                "description": "institution name",
                "label": "name",
                "input-type": "text",
                "pattern": "^[A-Za-z0-9s]+$"
            },
            "bio": {
                "type": "string",
                "label": "bio",
                "input-type": "text",
                "pattern": "^[A-Za-z0-9s]+$",
                "help-box": "tell us about yourself"
            },
question from:https://stackoverflow.com/questions/17597238/escaping-regex-to-get-valid-json

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Its just the slashes that are messing up the validation you could encode them using %5C which is the hex encoding of or what Mike W says you could double escape like \ and then you could just decode them when you want to use them


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...