Regex to validate JSON

前端 未结 12 2390
挽巷
挽巷 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:56

    I realize that this is from over 6 years ago. However, I think there is a solution that nobody here has mentioned that is way easier than regexing

    function isAJSON(string) {
        try {
            JSON.parse(string)  
        } catch(e) {
            if(e instanceof SyntaxError) return false;
        };  
        return true;
    }
    

提交回复
热议问题