Created
February 24, 2022 04:23
-
-
Save DavidWells/52a52efe7816fda9ce82e4a3e293941a to your computer and use it in GitHub Desktop.
Trim comments from a string
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
// via https://github.com/suchipi/without-comments/blob/main/index.js | |
function trimComments(content, commentToken = "#") { | |
const startRegex = new RegExp(`^${commentToken}`); | |
const endRegex = new RegExp(`${commentToken}.*$`, "g"); | |
return content | |
.split("\n") | |
.map((line) => { | |
// remove comment lines | |
if (startRegex.test(line.trim())) return ""; | |
// remove trailing comments | |
line = line.replace(endRegex, ""); | |
return line; | |
}) | |
.filter((line) => line != null) | |
.join("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment