Skip to content

Instantly share code, notes, and snippets.

@Kasahs
Created September 26, 2018 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kasahs/b17f0651ebb9907206d0ac03a3852f04 to your computer and use it in GitHub Desktop.
Save Kasahs/b17f0651ebb9907206d0ac03a3852f04 to your computer and use it in GitHub Desktop.
regex for matching lines that are c style comments
const regex = /^\/\/.*|\/\*(.|\s)*?\*\//gm;
const str = `/**
asdnajsndjk
*/
// asdaskdbjkasdasdasdbjk
asdasdasdasdbjhbcjahbhj
asdbhkbajkbkcbasdasdas
//asdnbakjnbjknbjkansd
/**
asdnjkanskjdbnkjbjkbdkbjkbjkabsd
*/`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment