Strip multi-line (star) comments from JavaScript, while leaving any CDATA sections intact.

Published on Tue Mar 01 2022

This regular expression removes multi-line (star) comments from JavaScript, while leaving any CDATA sections intact. Use the global (g) modifier to match them all (if you plan to use PHP's preg_replace, the g modifier is not necessary), and use the (s) modifier to make dots match newlines (this will be required in for using with PHP's preg_replace function). This regular expression does not exhaust all possibilities (such as a space between the opening or closing star comment and the CDATA tag), but it functions if the style guidelines are of the more popularized variety.

Additional matching regexes for
Strip multi-line (star) comments from JavaScript, while leaving any CDATA sections intact.