REGEX:如何使用PHP代码从javascripts中删除注释 [英] REGEX: How to remove comments from javascripts, using PHP code

查看:216
本文介绍了REGEX:如何使用PHP代码从javascripts中删除注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将所有javascriupt合并到一个整齐的文件中以降低http请求!我卡住了删除评论 / * comments * / // comments
我的水平远远低于缩小或解析的东西。我知道如何制作通心粉串。任何比这更复杂的东西,你都找不到我的电脑或厨房,SO:

I am combining all my javascriupt into one neat file in order to lower http requests! Im stuck removing the comments /* comments */ and // comments. My level is by far below minification or parsing stuff. I know how to make macaroni strings. Anything more complex than that, you will not find in my computer or kitchen, SO:

同时将它合并到一个文件,我想删除所有评论。

这是什么正确的正则表达式?

meanwhile at combining it to one file, i want to remove all comments.
What is the correct regex for this?

<?php
header('Content-type: text/javascript');    
$offset = 60 * 60 * 24; // Cache for a day
header ('Cache-Control: max-age=' . $offset . ', must-revalidate');
header ('Expires: ' . gmdate ("D, d M Y H:i:s", time() + $offset) . ' GMT');

ob_start("compress");
function compress($buffer) {

# NOT SURE, not all new lines are removed??
# remove tabs, spaces, newlines, etc.
$buffer = str_replace(array("\r\n", "\r", "\t", '  ', '    '), '', $buffer);  

# WORKS !!!
# remove comments / XXXXXXX
$buffer = preg_replace('(// .+)', '', $buffer);

######################################################################## 
# !! STUCK HERE !! OUTPUT FILE LOOKS OK BUT WEBSITE DOESNT LOAD OK IF THIS IS ON
# remove comments / * XXX  (enters etc) XXXX  * /
# $buffer = preg_replace('#/\*(?:.(?!/)|[^\*](?=/)|(?<!\*)/)*\*/#s', '', $buffer);
########################################################################        

return $buffer;
}

include('../file1.js');
include('../file2.js');  
ob_end_flush();
?>

如果能抓住并删除以下内容会很棒:

It would be great if it would catch and delete the following:

/* XXXX */

/* 
  XXXX
  XXXX
*/

这就是全部!无法使用正常工作,甚至使用这个令人难以置信的工具,我发现正确的匹配是:

Thats all! Cant get it to work nomatter what regex i use even with this incredible tool, where i FOUND the right match to be:

RegExp: /\/\*(?:.(?!/)|[^\*](?=/)|(?<!\*)/)*\*\//gs
pattern: \/\*(?:.(?!/)|[^\*](?=/)|(?<!\*)/)*\*\/
flags: gs

http://gskinner.com/RegExr/

推荐答案

使用正则表达式不是删除Javascript注释的最有效方法。你需要一个字符串解析器和minifier。请参阅 http://razorsharpcode.blogspot.com/2010/02 /lightweight-javascript-and-css.html

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

如果您坚持使用正则表达式模式,请考虑如何解析这个不包含Javascript注释的简单代码完全:

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;

注意 alert()之前的双斜线。使用正则表达式的愚蠢解析器会将有效的Javascript代码视为注释!

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

这篇关于REGEX:如何使用PHP代码从javascripts中删除注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆