How to remove JS comments using PHP?
This question is updated: Nov. 4 2013 and answered by: Alexander Yancharuk
But there is a problem right now. A new code: id = id.r
I would like to share a regex (PHP) snippet of code I wrote for myself it is also being used in the YIREO sriptmerge plugin for joomla marked as simple code.. To compress javascript code and remove all comments from it. It also works with mootools It is fast (in comparison to other PHP solutions) and does not damage the JavaScript it self and it resolves lots of comment removal issues.
If you click on the link below you find a comment removal script in regex.
These are 112 lines off code that work together also works with mootools and Joomla and drupal and other cms websites. Tested it on 800.000 lines of code and comments. works fine. This one also selects multiple parenthetical like ( abc(/nn/('/xvx/'))"// testing line") and comments that are between colons and protect them. 23-01-2016..! This is the code with the comments in it.!!!!
Click Here
I have just for the fun of it tested this
(var regex=/(ftp|https?):\/\//;alert('hello,world');
) and this
/* foo(); // some comment */
and this: "Python's division: 1 // 2"
and this //"you dope"
example
and my compression code those not damage the example above!
COMMENT FROM ABOVE:(
A stupid parser that uses regular expressions will treat valid JavaScript code as comments!) So Maybe you can also write none stupid parsers with regex???
Please feel free to leave a comment if you find a problem with this script with valid javascript sentences. And any kind of comments tags combined (or not combined) that it does not solve correctly..
Updated needs no more testing 800.000 lines work fine now.!