Escaping Regex to get Valid JSON

后端 未结 2 1827
予麋鹿
予麋鹿 2020-12-03 02:39

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 flag

相关标签:
2条回答
  • 2020-12-03 03:06

    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

    0 讨论(0)
  • 2020-12-03 03:18

    The accepted answer doesn't work for me. %5C doesn't work well with a linter. Plus manually doing it is a job. How about a very long regex -

    ^(([^<>()[\\]\\.,;:\\s@\"]+(\\.[^<>()[\]\\.,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$

    So, please don't kill yourself and head to this get this done - https://www.freeformatter.com/json-escape.html#ad-output

    In case the link doesn't work in future, please find some other online tool :)

    0 讨论(0)
提交回复
热议问题