Regular Expressions 101

Community Library

Community Library Entry

3

Regular ExpressionOpen Workspace

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

Description
Created·2013-03-21 20:42
Type·Match
Flavor·PCRE (Legacy)

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
Open Workspace