Last active
January 11, 2023 20:50
-
-
Save terrisgit/d78abbdca1b5533535f4da14b3217485 to your computer and use it in GitHub Desktop.
Remove C-style comments from a string in JavaScript
This file contains hidden or 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
| // 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