Regex to validate JSON

前端 未结 12 2446
挽巷
挽巷 2020-11-22 11:37

I am looking for a Regex that allows me to validate json.

I am very new to Regex\'s and i know enough that parsing with Regex is bad but can it be used to validate?

12条回答
  •  庸人自扰
    2020-11-22 11:58

    For "strings and numbers", I think that the partial regular expression for numbers:

    -?(?:0|[1-9]\d*)(?:\.\d+)(?:[eE][+-]\d+)?
    

    should be instead:

    -?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+\-]?\d+)?
    

    since the decimal part of the number is optional, and also it is probably safer to escape the - symbol in [+-] since it has a special meaning between brackets

提交回复
热议问题