Some day I wanted to check if the string is valid of JSON format. After some research I found the solution:
/**
* Function isJSON
* Check if a string is JSON
*/
if(!String.prototype.isJSON)
{
String.prototype.isJSON = function()
{
return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(this.replace(/"(\\.|[^"\\])*"/g,'')));
}
}
Leave a Reply