Regular Expressions 101

Community Patterns

1...34567...901

Community Library Entry

4

Regular Expression
PCRE (PHP <7.3)

/
\/\*(?!(\<\!\[CDATA\[)|(\]\]>))(.*?)\*\/
/
gs

Description

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.

Submitted by Dane MacMillan - 12 years ago