REGEX: How to remove comments from javascripts, using PHP code

前端 未结 4 818
予麋鹿
予麋鹿 2020-12-20 03:47

I am combining all my javascriupt into one neat file in order to lower http requests! Im stuck removing the comments /* comments */ and // comments

4条回答
  •  孤城傲影
    2020-12-20 04:21

    Using regular expressions isn't the most efficient way of removing Javascript comments. You need a string parser and minifier. See http://razorsharpcode.blogspot.com/2010/02/lightweight-javascript-and-css.html

    If you insist on regex patterns, think about how you would parse this simple code that contains no Javascript comments at all:

    var regex=/(ftp|https?):\/\//; alert('hello, world'); return regex;
    

    Notice the double slash before alert(). A stupid parser that uses regular expressions will treat valid Javascript code as comments!

提交回复
热议问题