JavaScript: extract block comments

A regex example about \/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$.

Original regex demonstration for simple input. A regular expression cannot correctly parse comments inside every string or template literal. Use a JavaScript parser for source transformations.

const source = "/* first */ const x = 1; /* second */";
const comments = [...source.matchAll(/\/\*[\s\S]*?\*\//g)]
  .map(match => match[0]);
console.log(comments); // ["/* first */", "/* second */"]