Skip to content

Instantly share code, notes, and snippets.

@6502
Created August 3, 2014 06:45
Show Gist options
  • Save 6502/8242e70a751a7f0a41fc to your computer and use it in GitHub Desktop.
Save 6502/8242e70a751a7f0a41fc to your computer and use it in GitHub Desktop.
Stripping whitespace and c-style comments from otherwise valid JSON data
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 : ""; });
}
@6502
Copy link
Author

6502 commented Aug 3, 2014

The idea of the regexp is processing the source in chunks where a chunk is either

  • removable spacing (space, tab, newline and carriage return)
  • a multi-line /* ... */ C comment (line continuation rules of C are not supported)
  • a single-line // ... C comment
  • a double-quoted string
  • a sequence of non-dangerous characters (i.e. anything but spacing, double quotes or "/")

if the chunk starts with spacing or "/" is discarded, otherwise is copied

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment