Skip to content

Instantly share code, notes, and snippets.

@terrisgit
Last active January 11, 2023 20:50
Show Gist options
  • Select an option

  • Save terrisgit/d78abbdca1b5533535f4da14b3217485 to your computer and use it in GitHub Desktop.

Select an option

Save terrisgit/d78abbdca1b5533535f4da14b3217485 to your computer and use it in GitHub Desktop.
Remove C-style comments from a string in JavaScript
// Removes all /* C-style comments */ from a string
// Credit:
// https://blog.ostermiller.org/finding-comments-in-source-code-using-regular-expressions/
// However this fixes the input string /* comment one */ blah /* comment two */ by using nongreedy match
// This intentionally doesn't work with //-style comments because it was written for MySQL comments only
'some string'.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*?\*+\//g, '');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment