Regular Expressions 101

Community Patterns

Community Library Entry

3

Regular Expression
Created·2013-03-21 20:42
Flavor·PCRE (Legacy)

/
\/\*(?!(\<\!\[CDATA\[)|(\]\]>))(.*?)\*\/
/
gs
Open regex in editor

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