As part of the source code analysis program, it was necessary to cut all string literals from JavaScript. First, a state based parser was implemented in PHP, but it was slow and dull. And to make it quickly turned out using regular expressions. The inspiration was the post at stackoverflow. The result is the following solution: return preg_replace('/( "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" # match double quoted string | \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' # match single quoted string | (?s:\\/\\*.*?\\*\\/) # multiline comments | \\/\\/.*?\\n # singleline comments | string.replace\\(\\/[^\\/\\\\]*(?:\\\\.[^\\/\\\\]*)*\\/ # an JS regexp )/x', '', $str);
And it is many times faster than the analyzer, fifty lines of which have sunk into oblivion.