Created
January 17, 2019 15:30
-
-
Save ezirmusitua/8dbc79d5acdf53ca70b04c29a5eb03bb to your computer and use it in GitHub Desktop.
[Validate json string format] Validate json string format #javascript #json #regex #node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function validateJsonStr(jsonStr) { | |
let content = jsonStr.replace(/\\["\\\/bfnrtu]/g, '@'); | |
content = content.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'); | |
content = content.replace(/(?:^|:|,)(?:\s*\[)+/g, ''); | |
return /^[\],:{}\s]*$/.test(jsonStr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment