- $parser->add( '/[ \t]+((\/[^\r\n\*][^\/\r\n\\\\]*(\\\\.[^\/\r\n\\\\]*)*\/(i|g)*))/', '$1' );
- $parser->add( '/([^\w\$\/\'"*)\?:](\/[^\r\n\*][^\/\r\n\\\\]*(\\\\.[^\/\r\n\\\\]*)*\/(i|g)*))/', '$1' );
- // Remove comments
- $parser->add( '/\/\*(.|[\r\n])*?\*\//' );
+ // Regular expression with whitespace before it
+ $parser->add(
+ '[ \t]+ ( ( \/' . // whitespace then start slash
+ '[^\r\n\*]' . // not a comment-start or line ending
+ '[^\/\r\n\\\\]*' . // a sequence of non-special characters
+ '(' .
+ '\\\\.' . // an escaped dot
+ '[^\/\r\n\\\\]*' . // a sequence of non-special characters
+ ')*' . // any number of the above
+ '\/(i|g)*' . // pattern end, optional modifier
+ ') )',
+ '$1' );
+ // Regular expression with an operator before it
+ $parser->add(
+ '( [^\w\$\/\'"*)\?:] (\/' . // certain kinds of punctuation and then start slash
+ '[^\r\n\*]' . // not a comment-start or line ending
+ '[^\/\r\n\\\\]*' . // a sequence of non-special characters
+ '(' .
+ '\\\\.' . // an escaped dot
+ '[^\/\r\n\\\\]*' . // a sequence of non-special characters
+ ')*' . // any number of the above
+ '\/(i|g)*)' . // pattern end, optional modifier
+ ')',
+ '$1' );
+
+ // C-style comment: use non-greedy repetition to find the end
+ $parser->add( '\/ \* ( . | [\r\n] )*? \* \/' );
+