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,'')));
    }
}

Comments

You can leave a response, or trackback from your own site.

Before you add comment see for rules.

Leave a Reply

Your email address will not be published. Required fields are marked *

0j1p1r