Created
August 3, 2014 06:45
-
-
Save 6502/8242e70a751a7f0a41fc to your computer and use it in GitHub Desktop.
Stripping whitespace and c-style comments from otherwise valid JSON data
This file contains 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 strip_comments(s) { | |
return s.replace(/[ \t\r\n]+|\/\*(?:[^*]|[*][^\/])*\*\/|\/\/[^\n]*\n|"(?:[^\\"]|\\.)*"|[^ \t\r\n"\/]+/g, | |
function(s){ return (" \t\r\n\/".indexOf(s[0])==-1) ? s : ""; }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The idea of the regexp is processing the source in chunks where a chunk is either
/* ... */
C comment (line continuation rules of C are not supported)// ...
C commentif the chunk starts with spacing or "/" is discarded, otherwise is copied