Skip to content

Instantly share code, notes, and snippets.

@Akronae
Created June 2, 2024 19:30
Show Gist options
  • Save Akronae/a66dfc56102646f32d50ad5c06e1160c to your computer and use it in GitHub Desktop.
Save Akronae/a66dfc56102646f32d50ad5c06e1160c to your computer and use it in GitHub Desktop.
Clean/sanitize a JSON file by removing comments and trailing commas.
function cleanTsConfig(content: string) {
return (
content
// Remove comments
.replace(
/(?:\r\n|\n|^)(?:[^'"])*?(?:'(?:[^\r\n\\']|\\'|[\\]{2})*'|"(?:[^\r\n\\"]|\\"|[\\]{2})*")*?(?:[^'"])*?(\/\*(?:[\s\S]*?)\*\/|\/\/.*)/g,
""
)
// Remove trailing commas
.replace(/,(\s+\])/gm, "$1")
.replace(/,(\s+\})/gm, "$1")
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment