$text =~ s/( (?:(?:[^\/][^\/]*?|)*? # anything, except comment symbol ("|').*?\2 # quoted string (and take everything) (?:[^\/][^\/]*?|)*? # anything, except comment symbol )*? ) (?: \/\/ # comment symbol [^\\]* # anything, except continuation )??$ /$1/x; print $text, "\n";