Skip to content

Instantly share code, notes, and snippets.

@ezirmusitua
Created January 17, 2019 15:30
Show Gist options
  • Save ezirmusitua/8dbc79d5acdf53ca70b04c29a5eb03bb to your computer and use it in GitHub Desktop.
Save ezirmusitua/8dbc79d5acdf53ca70b04c29a5eb03bb to your computer and use it in GitHub Desktop.
[Validate json string format] Validate json string format #javascript #json #regex #node
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