$text =~ s/]> # the opening tag .*? # as few chars as possible until... (?:|$) # closing script tag or the end of the text ($) //xgis; # (x) comments (g)lobal, case (i)nsensitive, # treat string as (s)ingle line # uncommented version $text =~ s/]>.*?(?:|$)//gis;