转义正则表达式以获取有效的JSON [英] Escaping Regex to get Valid JSON

查看:368
本文介绍了转义正则表达式以获取有效的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模式中,我想识别某些模式以限制用户可以输入的数据类型.我使用正则表达式来限制用户可以输入的内容,但是当我尝试使用在线验证程序来验证JSON时,正则表达式会被标记,例如 one .

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?

奇怪的是,验证器仅在某些实例上触发.例如,它标记了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-9\s]+$"
            },
            "bio": {
                "type": "string",
                "label": "bio",
                "input-type": "text",
                "pattern": "^[A-Za-z0-9\s]+$",
                "help-box": "tell us about yourself"
            },

推荐答案

只是斜线弄乱了验证,您可以使用%5C对其进行编码,这是\的十六进制编码或Mike W所说的可以像\\一样双重转义,然后您就可以在想要使用它们时对其进行解码

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

这篇关于转义正则表达式以获取有效的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆