REGEX:如何使用 PHP 代码从 javascript 中删除注释

发布于 2024-10-06 23:20:22 字数 1674 浏览 4 评论 0原文

我将所有 javascriupt 合并到一个整洁的文件中,以减少 http 请求!我一直在删除注释 /* comments */// comments。 我的水平远远低于缩小或解析的水平。我知道怎么做通心粉串。任何比这更复杂的东西,你在我的电脑或厨房中都找不到,所以:

问题

同时将其合并到一个文件中,我想删除所有评论。
正确的正则表达式是什么?

<?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();
?>

如果它能够捕获并删除以下内容,那就太好了:

/* XXXX */

/* 
  XXXX
  XXXX
*/

仅此而已!无论我使用什么正则表达式,即使使用这个令人难以置信的工具,也无法让它工作,我发现正确的匹配是:

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

http:// /gskinner.com/RegExr/

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:

QUESTION

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/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╰つ倒转 2024-10-13 23:20:22

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

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

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

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

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!

奢望 2024-10-13 23:20:22

我想分享一段正则表达式(PHP)代码
我为自己写的,它也被用于
joomla 的 YIREO sriptmerge 插件标记为简单代码..
压缩 JavaScript 代码并删除其中的所有注释。
它也可以与 mootools 一起使用,速度很快
(与其他 PHP 解决方案相比)并且不会损坏
JavaScript 本身解决了许多评论删除问题。

//--------------------------------------------------------------------------------------------------------- 
//Just started with it!
//Prototype Still working at it when ever I got time by patient please? Tested on 800.000 lines of code!
// Finished it 800.000 lines remove javascript comments without error....16-01-2016
// Its not bullentproof but chances are its gonna work for you...
//----------------------------------------------------------------//-------- //------------------------------------------------------------------------------
//--------------------------------------------------------------------------
//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 selects multiple parenthetical like with comments between colons ( abc(/*nn*/('/*xvx*/'))"// testing line") and protect them.
//16-01-2016..! This is the code with the comments in it.!!!!    
//----------------------------------------------------------------//-----------------------------------------
$buffer = preg_replace("/(\*\/\s*)\/\/(?!(\*\/|[^\r\n]*?[\\\\n\\\\r]+\s*\"\s*\+|[^\r\n]*?[\\\\n\\\\r]+\s*\'\s*\+))[^\n\r\;]*?[^\;]\s*[\n\r]/", "$1\n", $buffer);
do {$buffer = preg_replace("/(http(s)?\:)([^\r\n]*?)(\/\/)/", "$1$3qDdXX", $buffer, 1, $count);} while ($count);
do {$buffer = preg_replace("/(^^\s*\/)(\/).*/", "\n", $buffer, 1, $count);} while ($count);
$buffer = preg_replace("/([\r\n]+?\s*|\,\s*|\;\s*|\|\s*|\)\s*|\+\s*|\&\s*|\{\s*|\}\s*|\]\s*|\[\s*|\+\s*|\'\s*|\"\s*|\:\s*|-\s*)((\/)(\/)+)([^\r\n\'\"]*?[nte]'[a-z])*?(?!([^\r\n]*?)([\'\"]|[\\\\]|\*\/|[=]+\s*\";|[=]+\s*\';)).*/", "$1\n", $buffer);
$buffer = preg_replace("/(^^\s*\/\*)(?!([\'\"]))[\s\S]*?(\*\/)/", "\n \n", $buffer);    
$buffer = preg_replace("/(\|\|\s*|=\s*|[\n\r]|\;\s*|\,\s*|\{\s*|\}\s*|\+\s*|\?\s*)((?!([\'\"]))\/\*)(?!(\*\/))[\s\S]*?(\*\/)/", "$1\n", $buffer);
$buffer = preg_replace("/(\;\s*|\,\s*|\{\s*|\}\s*|\+\s*|\?\s*|[\n\r]\s*)((?!([\'\"]))\/\*)(?!(\*\/))[\s\S]*?(\*\/)/", "$1\n", $buffer);
//Remove: ) /* non-empty*//*xxx*/)
do {$buffer = preg_replace('/([^\/\"\'\*a-zA-Z0-9\>])\/\*(?!(\*\/))[^\n\r@]*?\*\/(?=([\/\"\'\\\\\*a-zA-Z0-9\>\s=\)\(\,:;\.\}\{\|\]\[]))/', "$1", $buffer, 1, $count);} while ($count); 
$buffer = preg_replace("/([\;\n\r]\s*)\/\/.*/", "$1\n", $buffer);   
$buffer = preg_replace("/(\/\s\/)([g][\W])/", "ZUQQ$2", $buffer);
$buffer = preg_replace("/\\\\n/", "AQerT", $buffer);
$buffer = preg_replace("/\\\\r/", "BQerT", $buffer);    
////---------------------------------------------------------------------------------------------------------
// Remove all extra new lines after [ and \
$buffer = preg_replace("/([^\*])(\*|[\r\n]|\'|\"|\,|\+|\{|;|\(|\)|\[|\]|\{|\}|\?|[^p|s]:|\&|\%|[^\\\\][a-m-o-u-s-zA-Z]|\||-|=|[0-9])(\s*)(?!([^=\\\\\&\/\"\'\^\*:]))(\/)(\/)+(?!([\r\n\*\+\"]*?([^\r\n]*?\*\/|[^\r\n]*?\"\s*\+|([^\r\n]*?=\";))))([^\n\r]*)([^;\"\'\{\(\}\,]\s*[\\\\\[])(?=([\r\n]+))/", "$1$2$3", $buffer);

// /* followed by (not new line but) ... */ ... /* ... till */
$buffer = preg_replace("/((([\r\n]\s*)(\/\*[^\r\n]*?\*\/(?!([^\n\r]*?\"\s*\+)))([^\n\r]*?\/\*[^\n\r]*?\*\/(?!([^\n\r]*?\"\s*\+))[^\n\r]*?\/\*[^\n\r]*?\*\/(?!([^\n\r]*?\"\s*\+)))+)+(?!([\*]))(?=([^\n\r\/]*?\/\/\/)))/", "$3", $buffer);
// (slash slash) remove everything behinde it not if its followed by */ and /n/r or " + and /n/r
$buffer = preg_replace("/([\r\n]+?\s*)((\/)(\/)+)(?!([^\r\n]*?)([\\\\]|\*\/|[=]+\s*\";|[=]+\s*\';)).*/", "$1\n", $buffer);
// slash slash star between collons protect like: ' //* ' by TDdXX
$buffer = preg_replace("/(\'\s*)(\/\/\*)([^\r\n\*]*?(?!(\*\/))(\'))/", "$1TDdXX$3", $buffer); 
// slash slash star between collons protect like: " //* " by TDdXX
$buffer = preg_replace("/(\"\s*)(\/\/\*)([^\r\n\*]*?(?!(\*\/))(\"))/", "$1TDdXX$3", $buffer); 
// slash slash star between collons protect like: ' //* ' by TDdXX
$buffer = preg_replace("/(\'\s*)(\/\*)([^\r\n\*]*?(?!(\*\/))(\'))/", "$1pDdYX$3", $buffer); 
// slash slash star between collons protect like: " //* " by TDdXX
$buffer = preg_replace("/(\"\s*)(\/\*)([^\r\n\*]*?(?!(\*\/))(\"))/", "$1pDdYX$3", $buffer);
// in regex star slash protect by: ODdPK
$buffer = preg_replace("/(\,\s*)(\*\/\*)(\s*[\}\"\'\;\)])/", "$1RDdPK$3", $buffer); // , */* '
$buffer = preg_replace('/(\n|\r|\+|\&|\=|\|\||\(|[^\)]\:[^\=\,\/\$\\\\\<]|\(|return(?!(\/[a-zA-Z]+))|\!|\,)(?!(\s*\/\/|\n))(\s*\/)([^\]\)\}\*\;\,gi\.]\s*)([^\/\n]*?)(\*\/)/', '$1$4$5$6ODdPK', $buffer); 
//// (slash r) (slash n) protect if followed by " + and new line
$buffer = preg_replace("/[\/][\/]+(AQerTBQerT)(\s*[\"]\s*[\+])/", "WQerT", $buffer);
$buffer = preg_replace("/[\/][\/]+(\*\/AQerTBQerT)(\s*[\"]\s*[\+])/", "YQerT", $buffer);
// Html Text protection!
$buffer = preg_replace("/([\r\n]\s*\/\/)[^\r\n]*?\/\*(?=(\/))[^\r\n]*?([\r\n])/", "$1 */$3", $buffer);
$buffer = preg_replace("/([\)]|[^\/|\\\\|\"])(\/\*)(?=([^\r\n]*?[\\\\][rn]([\\\\][nr])?\s*\"\s*\+\s*(\n|\r)?\s*\"))/", "$1pDdYX", $buffer);
$buffer = preg_replace('/([\"]\s*[\,\+][\r\n]\s*[\"])(\s*\/\/)((\/\/)|(\/))*/', '$1qDdXX', $buffer);
$buffer = preg_replace('/([\"]\s*[\,\+][\r\n]\s*[\"](qDdXX))[\\\\]*(\s*\/\/)*((\/\/)|(\/))*/', '$1', $buffer);
// started by new line slash slash remove all not followed by */ and new line!
$buffer = preg_replace("/([\r\n]\s*)(?=([^\r\n\*\,\:\;a-zA-Z\"]*?))(\/)+(\/)[^\r\n\/][^\r\n\*\,]*?[\*]+(?!([^\r\n]*?(([^\r\n]*?\/|\"\s*\)\s*\;|\"\s*\;|\"\s*\,|\'\s*\)\s*\;|\'\s*\;|\'\s*\,))))[^\r\n]*(?!([\/\r\n]))[^\r\n]*/", "$1", $buffer);
// removes all *.../ achter // leaves the ( // /* staan en */ ) 1 off 2
$buffer = preg_replace("/([\r\n](\/)*[^:\;\,\.\+])(\/\/[^\r\n]*?)(\*)?([^\r\n]+?)(\*)+([^\r\n\*\/])+?(\/[^\*])(?!([^\r\n]*?((\"\s*\)\s*\;|\"\s*\;|\"\s*\,|\'\s*\)\s*\;|\'\s*\;|\'\s*\,))))/", "$1$3$7$8", $buffer);
// removes all /* after // leaves the ( // */ staan ) 2 off 2
do {$buffer = preg_replace("/([\r\n])((\/)*[^:\;\,\.\+])(\/\/[^\r\n]*?)(\*)?([^\r\n]+?)(\/|\*)([^\r\n]*?)(\*)[\r\n]/", "$1", $buffer, 1, $count);} while ($count); 
////---------------------------------------------------------------------------------------------------------
// removes all (/* and */) combinations after // and everything behinde it! but leaves  ///* */ or example. ///*//*/ one times.
$buffer = preg_replace("/(((([\r\n](?=([^:;,\.\+])))(\/)+(\/))(\*))([^\r\n]*?)(\/\*)*([^\r\n])*?(\*\/)(?!([^\r\n]*?((\"\s*\)\s*\;|\"\s*\;|\"\s*\,|\'\s*\)\s*\;|\'\s*\;|\'\s*\,))))(((?=([^:\;\,\.\+])))(\/)*([^\r\n]*?)(\*|\/)?([^\r\n]*?)(\/\*)([^\r\n])*?(\*\/)(?!([^\r\n]*?((\"\s*\)\s*\;|\"\s*\;|\"\s*\,|\'\s*\)\s*\;|\'\s*\;|\'\s*\,)))))*)+[^\r\n]*/", "$2$7$9$10$11$12", $buffer);
// removes /* ... followed by */ repeat even pairs till new line!
$buffer = preg_replace("/(\/\*[\r\n]\s*)(?!([^\/<>;:%~`#@&-_=,\.\$\^\{\[\(\|\)\*\+\?\'\"\a-zA-Z0-9]))(((\/\*)[^\r\n]*?(\*\/)?[^\r\n]*?(\/\*)[^\r\n]*?(\*\/))*((\/\*)[^\r\n]*?(\*\/)))+(?!([^\r\n]*?(\*\/|\/\*)))[^\r\n]*?[\r\n]/", "\n", $buffer);
////---------------------------------------------------------------------------------------------------------
// (Mark) Regex Find all "  Mark with = AwTc  and  CwRc // special cahacers are:  . \ + * ? ^ $ [ ] ( ) { } < > = ! | : " '
$buffer = preg_replace("/(?!([\r\n]))([^a-zA-Z0-9]\+|\?|&|\=|\|\||\!|\(|,|return(?!(\/[a-zA-Z]+))|[^\)]\:)(?!(\s*\/\/|\n|\/\*[^\r\n\*]*?\*\/))(\s*\/([\*\^]?))(?!([\r\n\*\/]|[\*]))(?!(\<\!\-\-))(([^\^\]\)\}\*;,g&\.\"\']?\s*)(?=([\]\)\}\*;,g&\.\/\"\']))?)(([^\r\n]*?)(([\w\W])([\*]?\/\s*)(\})|([^\\\\])([\*]?\/\s*)(\))|([\w\W])([\*]?\/\s*)([i][g]?[\W])|([\w\W])([\*]?\/\s*)([g][i]?[\W])|([\w\W])([\*]?\/\s*)(?=(\,))|([^\\\\]|[\/])([\*]?\/\s*)(;)|([\w\W])([\*]?\/\:\s)(?!([@\]\[\)\(\}\{\.,#%\+-\=`~\*&\^;\:\'\"]))|([^\\\\])([\*]?\/\s*)(\.[^\/])|([^\\\\])([\*]?\/\s*)([\r\n]\s*[;\.,\)\}\]]\s*[^\/]|[\r\n]\s*([i][g]?[\W])|[\r\n]\s*([g][i]?[\W])))|([^\\\\])([\*]?\/\s*)([;\.,\)\}\]]\s*[^\/]|([i][g]?[\W])|([g][i]?[\W])))/", "$2$3$5AwTc$7$8$10$13$15$18$21$24$27$30$33$36$39$44CwRc$16$17$19$20$22$23$25$26$28$31$32$34$35$37$38$40$41$45$46", $buffer);


// Remove all extra new lines after [ and \
$buffer = preg_replace("/([^;\"\'\{\(\}\,\/]\s*[^\/][\\\\\[]\s?)\s*([\r\n]+)/", "$1", $buffer); 
$buffer = preg_replace("/([\|\[])\s*([\|\]])/", "$1$2", $buffer);
// (star slash) or (slash star) 1 sentence! Protect! With pDdYX and ODdPK
do {$buffer = preg_replace('/(AwTc)([^\r\nC]*?)(\/\*)(?=([^\r\n]*?CwRc))/', '$1$2pDdYX', $buffer, 1, $count);} while ($count);
do {$buffer = preg_replace('/(AwTc)([^\r\nC]*?)(\*\/)(?=([^\r\n]*?CwRc))/', '$1$2ODdPK', $buffer, 1, $count);} while ($count);
// (slash slash) 1 sentence! Protect with: qDdXX
do {$buffer = preg_replace('/(AwTc)([^\r\nC]*?)(\/\/)(?=([^\r\n]*?CwRc))/', '$1$2qDdXX', $buffer, 1, $count);} while ($count); 

//---------------------------------------------------------------------------------------------------------
// DEZE WERKT !! multiple parentheticals counting for even ones!
 $buffer = preg_replace("/([^\(\/\"\']\s*)(?!\(\s*function)((\()(?=([^\n\r\)]*?[\'\"]))(?!([^\r\n]*?\"\s*\<[^\r\n]*?\>\s*\"|[^\r\n]*?\"\s*\\\\\s*\"|[^\r\n]*?\"\s*\[[^\r\n]*?\]\s*\"))((?>[^()]+)|(?2))*?\))(?!(\s*\"\s*\;|\s*\'\s*\;|\s*\/|\s*\)|\s*\"|[^\n\r]*?\"\s*\+\s*(\n|\r)?\s*\"))/", "$1 /*Yu*/ $2 /*Zu*/ ", $buffer);

// this one is  SINGLE parentheticals pair.
//     $buffer = preg_replace("/([^\(\/\"\']\s*)((\()(?=([^\n\r\)]*?[\'\"]))(?!(function|\)|[^\r\n]*?\"\s*\+[^\r\n]*?\+\s*\"|[^\r\n]*?\"\s*\<[^\r\n]*?\>\s*\"|[^\r\n]*?\"\s*\\\\\s*\"|[^\r\n]*?\"\s*\[[^\r\n]*?\]\s*\"))([^()]*?\)))(?!(\s*\"\s*\;|\s*\'\s*\;|\s*\/|\s*\)|\s*\"|[^\n\r]*?\"\s*\+\s*(\n|\r)?\s*\"))/", "$1 /*Yu*/ $2 /*Zu*/ ", $buffer); 

// (slash slash) 1 sentence! Protect with: qDdXX
do {$buffer = preg_replace('/(\/\*Yu\*\/)([^\r\n]*?)(\/)(\/)(?=([^\r\n]*?\/\*Zu\*\/))/', '$1$2qDdXX', $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(\/\*Yu\*\/)([^\n\r\'\"]*?[\"\'])([^\n\r\)]*?)(\/\*)([^\n\r\'\"\)]*?[\"\'])([^\n\r]*?\/\*Zu\*\/)/", "$1$2$3pDdYX$5$6", $buffer, 1, $count);} while ($count);
do {$buffer = preg_replace("/(\/\*Yu\*\/)([^\n\r\'\"]*?[\"\'])([^\n\r\)]*?)(\*\/)([^\n\r\'\"\)]*?[\"\'])([^\n\r]*?\/\*Zu\*\/)/", "$1$2$3ODdPK$5$6", $buffer, 1, $count);} while ($count);

//---------------------------------------------------------------------------------------------------------
// (slash slash) 2 sentences! Protect ' and "
do {$buffer = preg_replace("/(=|\+|\(|[a-z]|\,)(\s*)(\")([^\r\n\;\/\'\)\,\]\}\*]*?)(\/)(\/)([^\r\n\;\"\*]*?)(\")/", "$1$2$3$4qDdXX$7$8", $buffer, 1, $count);} while ($count);
do {$buffer = preg_replace("/(=|\+|\(|[a-z]|\,)(\s*)(\')([^\r\n\;\/\'\)\,\]\}\*]*?)(\/)(\/)([^\r\n\*\;\']*?)(\')/", "$1$2$3$4qDdXX$7$8", $buffer, 1, $count);} while ($count); 
// (slash slash) 2 sentences! Protect slash slash between ' and "
do {$buffer = preg_replace("/(\"[^\r\n\;]*?)(\/)(\/)([^\r\n\"\;]*?([\"]\s*(\;|\)|\,)))/", "$1qDdXX$4", $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(\'[^\r\n\;]*?)(\/)(\/)([^\r\n\'\;]*?([\']\s*(\;|\)|\,)))/", "$1qDdXX$4", $buffer, 1, $count);} while ($count); 
//---------------------------------------------------------------------------------------------------------
// Remove all slar slash achter \n
$buffer = preg_replace("/([\n\r])([^\n\r\*\,\"\']*?)(?=([^\*\,\:\;a-zA-Z\"]*?))(\/)(\/)+(?=([^\n\r]*?\*\/))([^\n\r]*?(\*\/)).*/", "$1$4$5 $8", $buffer); 
do {$buffer = preg_replace("/([\r\n]\s*)((\/\*(?!(\*\/)))([^\r\n]+?)(\*\/))(?!([^\n\r\/]*?(\/)(\/)+\*))/", "$1$3$6", $buffer, 1, $count);} while ($count);
$buffer = preg_replace("/([\n\r]\/)(\/)+([^\n\r]*?)(\*\/)([^\n\r]*?(\*\/))(?!([^\n\r]*?(\*\/)|[^\n\r]*?(\/\*))).*/", "$1/ $4", $buffer);  
do {$buffer = preg_replace("/([\n\r]\s*\/\*\*\/)([^\n\r=]*?\/\*[^\n\r]*?\*\/)(?=([\n\r]|\/\/))/", "$1", $buffer, 1, $count);} while ($count); 
$buffer = preg_replace("/([\n\r]\s*\/\*\*\/)([^\n\r=]*?)(\/\/.*)/", "$1$2", $buffer); 
// Remove all slash slash achter = '...'; //......
do {$buffer = preg_replace("/(\=\s*)(?=([^\r\n\'\"]*?\'[^\n\r\']*?\'))([^\n\r;]*?[;]\s*)(\/\/[^\r\n][^\r\n]*)[\n\r]/", "$1$3", $buffer, 1, $count);} while ($count);
// protect slash slash '...abc//...abc'!
do {$buffer = preg_replace("/(\=)(\s*\')([^\r\n\'\"]*?)(\/)(\/)([^\r\n]*?[\'])/", "$1$2$3qDdXX$6", $buffer, 1, $count);} while ($count);
//(slash star) or (star slash) : no dubble senteces here! Protect with: pDdYX and ODdPK
do {$buffer = preg_replace("/(\"[^\r\n\;\,\"]*?)(\/)(\*)(?!([YZ]u\*\/))([^\r\n;\,\"]*?)(\")/", "$1pDdYX$5$6", $buffer, 1, $count);} while ($count);   // open
do {$buffer = preg_replace("/([^\"]\"[^\r\n\;\/\,\"]*?)(\s*)(\*)(\/)([^\r\n;\,\"=]*?)(\")/", "$1$2ODdPK$5$6", $buffer, 1, $count);} while ($count); // close
do {$buffer = preg_replace("/(\'[^\r\n\;\,\']*?)(\/)(\*)(?!([YZ]u\*\/))([^\r\n;\,\']*?)(\')/", "$1pDdYX$5$6", $buffer, 1, $count);} while ($count);   // open
do {$buffer = preg_replace("/(\'[^\r\n\;\/\,\']*?)(\s*)(\*)(\/)([^\r\n;\,\']*?)(\')/", "$1$2ODdPK$5$6", $buffer, 1, $count);} while ($count); // close
// protect star slash '...abc*/...abc'!
do {$buffer = preg_replace("/(\'[^\r\n\;\,\']*?)(\*)(\/)([^\r\n;\,\']*?)(\')(?!([^\n\r\+]*?[\']))/", "$1ODdPK$4$5", $buffer, 1, $count);} while ($count); 
// protect star slash '...abc*/...abc'!
do {$buffer = preg_replace("/(\"[^\r\n\;\,\"]*?)(\*)(\/)([^\r\n;\,\"]*?)(\")(?!([^\n\r\+]*?[\"]))/", "$1ODdPK$4$5", $buffer, 1, $count);} while ($count);
 //---------------------------------------------------------------------------------------------------------
//// \n protect
do {$buffer = preg_replace("/(=\s*\"[^\n\r\"]*?)(\/\/)(?=([^\n\r]*?\"\s*;))/", "$1qDdXX", $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(=\s*\"[^\n\r\"]*?)(\/\*)(?!([YZ]u\*\/))(?=([^\n\r]*?\"\s*;))/", "$1pDdYX", $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(=\s*\"[^\n\r\"]*?)(\*\/)(?=([^\n\r]*?\"\s*;))/", "$1ODdPK", $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(=\s*\'[^\n\r\']*?)(\/\/)(?=([^\n\r]*?\'\s*;))/", "$1qDdXX", $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(=\s*\'[^\n\r\']*?)(\/\*)(?!([YZ]u\*\/))(?=([^\n\r]*?\'\s*;))/", "$1pDdYX", $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(=\s*\'[^\n\r\']*?)(\*\/)(?=([^\n\r]*?\'\s*;))/", "$1ODdPK", $buffer, 1, $count);} while ($count); 
//---------------------------------------------------------------------------------------------------------
// (Slash Slash) alle = " // " and = ' // ' replace by! qDdXX
do {$buffer = preg_replace("/(\=|\()(\s*\")([^\r\n\'\"]*?[\'][^\r\n\'\"]*?)(\/)(\/)([^\r\n\'\"]*?[\'])(\s*\'[^\r\n\'\"]*?)(\/\/|qDdXX)?([^\r\n\'\"]*?[\'][^\r\n\'\"]*?[\"])(?!(\'\)|\s*[\)]?\s*\+|\'))/", "$1$2$3qDdXX$6$7qDdXX$9$10", $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(\=|\()(\s*\')([^\r\n\'\"]*?[\"][^\r\n\'\"]*?)(\/)(\/)([^\r\n\'\"]*?[\"])(\s*\"[^\r\n\'\"]*?)(\/\/|qDdXX)?([^\r\n\'\"]*?[\"][^\r\n\'\"]*?[\'])(?!(\'\)|\s*[\)]?\s*\+|\'))/", "$1$2$3qDdXX$6$7qDdXX$9$10", $buffer, 1, $count);} while ($count); 
//---------------------------------------------------------------------------------------------------------
// (slash slash) Remove all also , or + not followed by */ and newline
$buffer = preg_replace("/([^\*])(\*|[\r\n]|[^\\\\]\'|[^\\\\]\"|\,|\+|\{|;|\(|\)|\[|\]|\{|\}|\?|[^p|s]:|\&|\%|[^\\\\][a-m-o-u-s-zA-Z]|\||-|=|[0-9])(\s*)(?!([^=\\\\\&\/\"\'\^\*:]))(\/)(\/)+(?!([\r\n\*\+\"]*?([^\r\n]*?\*\/|[^\r\n]*?\"\s*\+|([^\r\n]*?=\";)))).*/", "$1$2$3", $buffer);
// (slash slash star slash) Remove everhing behinde it not followed by */ or new line
$buffer = preg_replace("/(\/\/\*\/)(?!([\r\n\*\+\"]*?([^\r\n]*?\*\/|[^\r\n]*?\"\s*\+|([^\r\n]*?=\";)))).*/", "", $buffer);
// Remove almost all star comments except colon/**/
$buffer = preg_replace("/(?!([^\n\r]*?[\'\"]))(\s*<!--.*-->)(?!(<\/div>))[^\n\r]*?.*/","$2$4", $buffer);
$buffer = preg_replace("/([\n\r][^\n\r\*\,\"\']*?)(?=([^\*\,\:\;a-zA-Z\"]*?))(\/)(\/)+(?!([\r\n\*\+\"]*?([^\r\n]*?\*\/|[^\r\n]*?\"\s*\+|([^\r\n]*?=\";)))).*/", "$1", $buffer);
$buffer = preg_replace("/(?!([^\n\r]*?[\'\"]))(\s*<!--.*-->)(?!(<\/div>))[^\n\r]*?(\*\/)?.*/","", $buffer);
$buffer = preg_replace("/(<!--.*?-->)(?=(\s*<\/div>))/","", $buffer);
//---------------------------------------------------------------------------------------------------------
// Restore all
$buffer = preg_replace("/qDdXX/", "//", $buffer);  // Restore //
$buffer = preg_replace("/pDdYX/", "/*", $buffer);   // Restore 
$buffer = preg_replace("/ODdPK/", "*/", $buffer);   // Restore 
$buffer = preg_replace("/RDdPK/", "*/*", $buffer);   // Restore 
$buffer = preg_replace("/TDdXX/", "//*", $buffer);   // Restore */
$buffer = preg_replace('/WQerT/', '\\\\r\\\\n" +', $buffer);   // Restore \r\n" + 
$buffer = preg_replace('/YQerT/', '//*/\\\\r\\\\n" +', $buffer);   // Restore \r\n" + 
$buffer = preg_replace('/AQerT/', '\\\\n', $buffer);   // Restore \n" 
$buffer = preg_replace('/BQerT/', '\\\\r', $buffer);   // Restore \r"
$buffer = preg_replace("/ZUQQ/", "/ /", $buffer);
$buffer = preg_replace('/\s\/\*Zu\*\/\s/', '', $buffer);   // Restore \n"
$buffer = preg_replace('/\s\/\*Yu\*\/\s/', '', $buffer);   // Restore \n"
////---------------------------------------------------------------------------------------------------------
//// Remove all markings!
$buffer = preg_replace('/(AwTc)/', '', $buffer);  // Start most Regex!
$buffer = preg_replace('/(CwRc)/', '', $buffer);  // End Most regex!
// all \s and [\n\r] repair like they where!
$buffer = preg_replace("/([a-zA-Z0-9]\s?)\s*[\n\r]+(\s*[\)\,&]\s?)(\s*[\r\n]+\s*[\{])/", "$1$2$3", $buffer); 
$buffer = preg_replace("/([a-zA-Z0-9\(]\s?)\s*[\n\r]+(\s*[;\)\,&\+\-a-zA-Z0-9]\s?)(\s*[\{;a-zA-Z0-9\,&\n\r])/", "$1$2$3", $buffer); 
$buffer = preg_replace("/(\(\s?)\s*[\n\r]+(\s*function)/", "$1$2", $buffer);
$buffer = preg_replace("/(=\s*\[[a-zA-Z0-9]\s?)\s*([\r\n]+)/", "$1", $buffer); 
//-----------------------------------------------
$buffer = preg_replace("/([^\*\/\'\"]\s*)(\/\/\s*\*\/)/", "$1", $buffer);
//// Remove all /**/// .... Remove expept /**/ and followed by */ till newline!
$buffer = preg_replace("/(\/\*\*\/)(\/\/(?!([^\n\r]*?\*\/)).*)/", "$1", $buffer);
$buffer = preg_replace("/(\;\/\*\*\/)(?!([^\n\r]*?\*\/)).*/", "", $buffer);
$buffer = preg_replace("/(\/\/\\\\\*[^\n\r\"\'\/]*?[\n\r])/", "\r\n", $buffer);
$buffer = preg_replace("/([\r\n]\s*)(\/\*[^\r\n]*?\*\/(?!([^\r\n]*?\"\s*\+)))/", "$1", $buffer);
//Remove colon /**/
$buffer = preg_replace("/\/\*\*\/\s/", " ", $buffer);
$buffer = preg_replace("/(\=\s*)(?=([^\r\n\'\"]*?\'[^\n\r\'\"]*?\'))([^\n\r\/]*?)(\/\/[^\r\n\"\'][^\r\n]*[\'\"])(\/\*\*\/)[\n\r]/", "$1$3$4\n", $buffer);
$buffer = preg_replace("/(\=\s*)(?=([^\r\n\'\"]*?\"[^\n\r\'\"]*?\"))([^\n\r\/]*?)(\/\/[^\r\n\"\'][^\r\n]*[\'\"])(\/\*\*\/)[\n\r]/", "$1$3$4\n", $buffer);
//Remove colon //
$buffer = preg_replace("/([^\'\"ps\s]\s*)(\:[^\r\n\'\"\[\]]*?\'[^\n\r\'\"]*?\')([^\n\r\/a-zA-Z0-9]*?)(\/\/)[^\r\n\/\'][^\r\n]*/", "$1$2", $buffer);
$buffer = preg_replace("/([^\'\"ps\s]\s*)(\:[^\r\n\'\"\[\]]*?\"[^\n\r\'\"]*?\")([^\n\r\/a-zA-Z0-9]*?)(\/\/)[^\r\n\/\"][^\r\n]*/", "$1$2", $buffer);
$buffer = preg_replace("/(\"[^\n\r\'\"\+]*?\")([^\n\r\/a-zA-Z0-9]*?)(\/\/)(?!(\*|[^\r\n]*?[\\\\n\\\\r]+\s*\"\s*\+|[^\r\n]*?[\\\\n\\\\r]+\s*\'\s*\+))[^\r\n\/\"][^\r\n]*/", "$1$2", $buffer);
//Remove all after ; slah slah+
$buffer = preg_replace("/(;\s*)\/\/(?!([^\n\r]*?\"\s*;)).*/", "$1 \n", $buffer);
//Remove: ) /* non-empty*//*xxx*/)
$buffer = preg_replace('/([\n\r][^\n\r\"]*?)([^\/\"\'\*\>])\/\*(?!(\*\/))[^\n\r\"]*?[^@]\*\//', "$1$2", $buffer);
//Remove // vooraf gegaan door: || | ? | , //
$buffer = preg_replace("/(\|\||[\?]|\,)(\s*)\/\/(?!([^\n\r]*?\*\/|\"|\')).*/", "$1$2", $buffer);
//Remove: [\n\r] after: [ \|\[\;\,\:\=\-\{\}\]\[\?\)\( ]
$buffer = preg_replace("/([\|\[\;\,\:\=\-\{\}\]\[\?\)\(])\s*[\n\r]\s*[\n\r](\s*[\n\r])+/", "$1\n", $buffer);
////---------------------------------------------------------------------------------------------------------
//END Remove comments.    //START Remove all whitespaces    
$buffer = preg_replace('/(--\s+\>)/', 'HwRc', $buffer);  // protect space between: -- >
//-------------------------------------------------------- 
$buffer = preg_replace('/\s+/', ' ', $buffer);
$buffer = preg_replace('/\s*(?:(?=[=\-\+\|%&\*\)\[\]\{\};:\,\.\<\>\!\@\#\^`~]))/', '', $buffer);
$buffer = preg_replace('/(?:(?<=[=\-\+\|%&\*\)\[\]\{\};:\,\.\<\>\?\!\@\#\^`~]))\s*/', '', $buffer);
$buffer = preg_replace('/([^a-zA-Z0-9\s\-=+\|!@#$%^&*()`~\[\]{};:\'",<.>\/?])\s+([^a-zA-Z0-9\s\-=+\|!@#$%^&*()`~\[\]{};:\'",<.>\/?])/', '$1$2', $buffer);
//-------------------------------------------------------
$buffer = preg_replace('/(HwRc)/', '-- >', $buffer);  // Repair space between: -- >
//END Remove all whitespaces

我只是为了好玩测试了这个
(var regex=/(ftp|https?):\/\//;alert('hello,world');) 和这个
<代码>/* foo(); // 一些注释 */ 以及:“Python 的除法:1 // 2”
和这个 //"you dope" 示例
我的压缩代码不会损坏上面的示例!
上面的评论:(
使用正则表达式的愚蠢解析器会将有效的 JavaScript 代码视为注释!)所以也许您也可以使用正则表达式编写不愚蠢的解析器???

我很粗俗,不是程序员,只是想
让插件像我想要的那样工作......
如果您找到了,请随时发表评论
该脚本与有效的 javascript 语句存在问题。以及任何一种
评论标签组合(或未组合),它不能正确解决。
因为我确实有很大的便利性,所以我现在没有任何 Javascript 语法
所以我不知道会发生什么,我只是在我能找到的例子上测试了它!

更新后无需再测试 8000.000 行现在工作正常。!

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.

//--------------------------------------------------------------------------------------------------------- 
//Just started with it!
//Prototype Still working at it when ever I got time by patient please? Tested on 800.000 lines of code!
// Finished it 800.000 lines remove javascript comments without error....16-01-2016
// Its not bullentproof but chances are its gonna work for you...
//----------------------------------------------------------------//-------- //------------------------------------------------------------------------------
//--------------------------------------------------------------------------
//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 selects multiple parenthetical like with comments between colons ( abc(/*nn*/('/*xvx*/'))"// testing line") and protect them.
//16-01-2016..! This is the code with the comments in it.!!!!    
//----------------------------------------------------------------//-----------------------------------------
$buffer = preg_replace("/(\*\/\s*)\/\/(?!(\*\/|[^\r\n]*?[\\\\n\\\\r]+\s*\"\s*\+|[^\r\n]*?[\\\\n\\\\r]+\s*\'\s*\+))[^\n\r\;]*?[^\;]\s*[\n\r]/", "$1\n", $buffer);
do {$buffer = preg_replace("/(http(s)?\:)([^\r\n]*?)(\/\/)/", "$1$3qDdXX", $buffer, 1, $count);} while ($count);
do {$buffer = preg_replace("/(^^\s*\/)(\/).*/", "\n", $buffer, 1, $count);} while ($count);
$buffer = preg_replace("/([\r\n]+?\s*|\,\s*|\;\s*|\|\s*|\)\s*|\+\s*|\&\s*|\{\s*|\}\s*|\]\s*|\[\s*|\+\s*|\'\s*|\"\s*|\:\s*|-\s*)((\/)(\/)+)([^\r\n\'\"]*?[nte]'[a-z])*?(?!([^\r\n]*?)([\'\"]|[\\\\]|\*\/|[=]+\s*\";|[=]+\s*\';)).*/", "$1\n", $buffer);
$buffer = preg_replace("/(^^\s*\/\*)(?!([\'\"]))[\s\S]*?(\*\/)/", "\n \n", $buffer);    
$buffer = preg_replace("/(\|\|\s*|=\s*|[\n\r]|\;\s*|\,\s*|\{\s*|\}\s*|\+\s*|\?\s*)((?!([\'\"]))\/\*)(?!(\*\/))[\s\S]*?(\*\/)/", "$1\n", $buffer);
$buffer = preg_replace("/(\;\s*|\,\s*|\{\s*|\}\s*|\+\s*|\?\s*|[\n\r]\s*)((?!([\'\"]))\/\*)(?!(\*\/))[\s\S]*?(\*\/)/", "$1\n", $buffer);
//Remove: ) /* non-empty*//*xxx*/)
do {$buffer = preg_replace('/([^\/\"\'\*a-zA-Z0-9\>])\/\*(?!(\*\/))[^\n\r@]*?\*\/(?=([\/\"\'\\\\\*a-zA-Z0-9\>\s=\)\(\,:;\.\}\{\|\]\[]))/', "$1", $buffer, 1, $count);} while ($count); 
$buffer = preg_replace("/([\;\n\r]\s*)\/\/.*/", "$1\n", $buffer);   
$buffer = preg_replace("/(\/\s\/)([g][\W])/", "ZUQQ$2", $buffer);
$buffer = preg_replace("/\\\\n/", "AQerT", $buffer);
$buffer = preg_replace("/\\\\r/", "BQerT", $buffer);    
////---------------------------------------------------------------------------------------------------------
// Remove all extra new lines after [ and \
$buffer = preg_replace("/([^\*])(\*|[\r\n]|\'|\"|\,|\+|\{|;|\(|\)|\[|\]|\{|\}|\?|[^p|s]:|\&|\%|[^\\\\][a-m-o-u-s-zA-Z]|\||-|=|[0-9])(\s*)(?!([^=\\\\\&\/\"\'\^\*:]))(\/)(\/)+(?!([\r\n\*\+\"]*?([^\r\n]*?\*\/|[^\r\n]*?\"\s*\+|([^\r\n]*?=\";))))([^\n\r]*)([^;\"\'\{\(\}\,]\s*[\\\\\[])(?=([\r\n]+))/", "$1$2$3", $buffer);

// /* followed by (not new line but) ... */ ... /* ... till */
$buffer = preg_replace("/((([\r\n]\s*)(\/\*[^\r\n]*?\*\/(?!([^\n\r]*?\"\s*\+)))([^\n\r]*?\/\*[^\n\r]*?\*\/(?!([^\n\r]*?\"\s*\+))[^\n\r]*?\/\*[^\n\r]*?\*\/(?!([^\n\r]*?\"\s*\+)))+)+(?!([\*]))(?=([^\n\r\/]*?\/\/\/)))/", "$3", $buffer);
// (slash slash) remove everything behinde it not if its followed by */ and /n/r or " + and /n/r
$buffer = preg_replace("/([\r\n]+?\s*)((\/)(\/)+)(?!([^\r\n]*?)([\\\\]|\*\/|[=]+\s*\";|[=]+\s*\';)).*/", "$1\n", $buffer);
// slash slash star between collons protect like: ' //* ' by TDdXX
$buffer = preg_replace("/(\'\s*)(\/\/\*)([^\r\n\*]*?(?!(\*\/))(\'))/", "$1TDdXX$3", $buffer); 
// slash slash star between collons protect like: " //* " by TDdXX
$buffer = preg_replace("/(\"\s*)(\/\/\*)([^\r\n\*]*?(?!(\*\/))(\"))/", "$1TDdXX$3", $buffer); 
// slash slash star between collons protect like: ' //* ' by TDdXX
$buffer = preg_replace("/(\'\s*)(\/\*)([^\r\n\*]*?(?!(\*\/))(\'))/", "$1pDdYX$3", $buffer); 
// slash slash star between collons protect like: " //* " by TDdXX
$buffer = preg_replace("/(\"\s*)(\/\*)([^\r\n\*]*?(?!(\*\/))(\"))/", "$1pDdYX$3", $buffer);
// in regex star slash protect by: ODdPK
$buffer = preg_replace("/(\,\s*)(\*\/\*)(\s*[\}\"\'\;\)])/", "$1RDdPK$3", $buffer); // , */* '
$buffer = preg_replace('/(\n|\r|\+|\&|\=|\|\||\(|[^\)]\:[^\=\,\/\$\\\\\<]|\(|return(?!(\/[a-zA-Z]+))|\!|\,)(?!(\s*\/\/|\n))(\s*\/)([^\]\)\}\*\;\,gi\.]\s*)([^\/\n]*?)(\*\/)/', '$1$4$5$6ODdPK', $buffer); 
//// (slash r) (slash n) protect if followed by " + and new line
$buffer = preg_replace("/[\/][\/]+(AQerTBQerT)(\s*[\"]\s*[\+])/", "WQerT", $buffer);
$buffer = preg_replace("/[\/][\/]+(\*\/AQerTBQerT)(\s*[\"]\s*[\+])/", "YQerT", $buffer);
// Html Text protection!
$buffer = preg_replace("/([\r\n]\s*\/\/)[^\r\n]*?\/\*(?=(\/))[^\r\n]*?([\r\n])/", "$1 */$3", $buffer);
$buffer = preg_replace("/([\)]|[^\/|\\\\|\"])(\/\*)(?=([^\r\n]*?[\\\\][rn]([\\\\][nr])?\s*\"\s*\+\s*(\n|\r)?\s*\"))/", "$1pDdYX", $buffer);
$buffer = preg_replace('/([\"]\s*[\,\+][\r\n]\s*[\"])(\s*\/\/)((\/\/)|(\/))*/', '$1qDdXX', $buffer);
$buffer = preg_replace('/([\"]\s*[\,\+][\r\n]\s*[\"](qDdXX))[\\\\]*(\s*\/\/)*((\/\/)|(\/))*/', '$1', $buffer);
// started by new line slash slash remove all not followed by */ and new line!
$buffer = preg_replace("/([\r\n]\s*)(?=([^\r\n\*\,\:\;a-zA-Z\"]*?))(\/)+(\/)[^\r\n\/][^\r\n\*\,]*?[\*]+(?!([^\r\n]*?(([^\r\n]*?\/|\"\s*\)\s*\;|\"\s*\;|\"\s*\,|\'\s*\)\s*\;|\'\s*\;|\'\s*\,))))[^\r\n]*(?!([\/\r\n]))[^\r\n]*/", "$1", $buffer);
// removes all *.../ achter // leaves the ( // /* staan en */ ) 1 off 2
$buffer = preg_replace("/([\r\n](\/)*[^:\;\,\.\+])(\/\/[^\r\n]*?)(\*)?([^\r\n]+?)(\*)+([^\r\n\*\/])+?(\/[^\*])(?!([^\r\n]*?((\"\s*\)\s*\;|\"\s*\;|\"\s*\,|\'\s*\)\s*\;|\'\s*\;|\'\s*\,))))/", "$1$3$7$8", $buffer);
// removes all /* after // leaves the ( // */ staan ) 2 off 2
do {$buffer = preg_replace("/([\r\n])((\/)*[^:\;\,\.\+])(\/\/[^\r\n]*?)(\*)?([^\r\n]+?)(\/|\*)([^\r\n]*?)(\*)[\r\n]/", "$1", $buffer, 1, $count);} while ($count); 
////---------------------------------------------------------------------------------------------------------
// removes all (/* and */) combinations after // and everything behinde it! but leaves  ///* */ or example. ///*//*/ one times.
$buffer = preg_replace("/(((([\r\n](?=([^:;,\.\+])))(\/)+(\/))(\*))([^\r\n]*?)(\/\*)*([^\r\n])*?(\*\/)(?!([^\r\n]*?((\"\s*\)\s*\;|\"\s*\;|\"\s*\,|\'\s*\)\s*\;|\'\s*\;|\'\s*\,))))(((?=([^:\;\,\.\+])))(\/)*([^\r\n]*?)(\*|\/)?([^\r\n]*?)(\/\*)([^\r\n])*?(\*\/)(?!([^\r\n]*?((\"\s*\)\s*\;|\"\s*\;|\"\s*\,|\'\s*\)\s*\;|\'\s*\;|\'\s*\,)))))*)+[^\r\n]*/", "$2$7$9$10$11$12", $buffer);
// removes /* ... followed by */ repeat even pairs till new line!
$buffer = preg_replace("/(\/\*[\r\n]\s*)(?!([^\/<>;:%~`#@&-_=,\.\$\^\{\[\(\|\)\*\+\?\'\"\a-zA-Z0-9]))(((\/\*)[^\r\n]*?(\*\/)?[^\r\n]*?(\/\*)[^\r\n]*?(\*\/))*((\/\*)[^\r\n]*?(\*\/)))+(?!([^\r\n]*?(\*\/|\/\*)))[^\r\n]*?[\r\n]/", "\n", $buffer);
////---------------------------------------------------------------------------------------------------------
// (Mark) Regex Find all "  Mark with = AwTc  and  CwRc // special cahacers are:  . \ + * ? ^ $ [ ] ( ) { } < > = ! | : " '
$buffer = preg_replace("/(?!([\r\n]))([^a-zA-Z0-9]\+|\?|&|\=|\|\||\!|\(|,|return(?!(\/[a-zA-Z]+))|[^\)]\:)(?!(\s*\/\/|\n|\/\*[^\r\n\*]*?\*\/))(\s*\/([\*\^]?))(?!([\r\n\*\/]|[\*]))(?!(\<\!\-\-))(([^\^\]\)\}\*;,g&\.\"\']?\s*)(?=([\]\)\}\*;,g&\.\/\"\']))?)(([^\r\n]*?)(([\w\W])([\*]?\/\s*)(\})|([^\\\\])([\*]?\/\s*)(\))|([\w\W])([\*]?\/\s*)([i][g]?[\W])|([\w\W])([\*]?\/\s*)([g][i]?[\W])|([\w\W])([\*]?\/\s*)(?=(\,))|([^\\\\]|[\/])([\*]?\/\s*)(;)|([\w\W])([\*]?\/\:\s)(?!([@\]\[\)\(\}\{\.,#%\+-\=`~\*&\^;\:\'\"]))|([^\\\\])([\*]?\/\s*)(\.[^\/])|([^\\\\])([\*]?\/\s*)([\r\n]\s*[;\.,\)\}\]]\s*[^\/]|[\r\n]\s*([i][g]?[\W])|[\r\n]\s*([g][i]?[\W])))|([^\\\\])([\*]?\/\s*)([;\.,\)\}\]]\s*[^\/]|([i][g]?[\W])|([g][i]?[\W])))/", "$2$3$5AwTc$7$8$10$13$15$18$21$24$27$30$33$36$39$44CwRc$16$17$19$20$22$23$25$26$28$31$32$34$35$37$38$40$41$45$46", $buffer);


// Remove all extra new lines after [ and \
$buffer = preg_replace("/([^;\"\'\{\(\}\,\/]\s*[^\/][\\\\\[]\s?)\s*([\r\n]+)/", "$1", $buffer); 
$buffer = preg_replace("/([\|\[])\s*([\|\]])/", "$1$2", $buffer);
// (star slash) or (slash star) 1 sentence! Protect! With pDdYX and ODdPK
do {$buffer = preg_replace('/(AwTc)([^\r\nC]*?)(\/\*)(?=([^\r\n]*?CwRc))/', '$1$2pDdYX', $buffer, 1, $count);} while ($count);
do {$buffer = preg_replace('/(AwTc)([^\r\nC]*?)(\*\/)(?=([^\r\n]*?CwRc))/', '$1$2ODdPK', $buffer, 1, $count);} while ($count);
// (slash slash) 1 sentence! Protect with: qDdXX
do {$buffer = preg_replace('/(AwTc)([^\r\nC]*?)(\/\/)(?=([^\r\n]*?CwRc))/', '$1$2qDdXX', $buffer, 1, $count);} while ($count); 

//---------------------------------------------------------------------------------------------------------
// DEZE WERKT !! multiple parentheticals counting for even ones!
 $buffer = preg_replace("/([^\(\/\"\']\s*)(?!\(\s*function)((\()(?=([^\n\r\)]*?[\'\"]))(?!([^\r\n]*?\"\s*\<[^\r\n]*?\>\s*\"|[^\r\n]*?\"\s*\\\\\s*\"|[^\r\n]*?\"\s*\[[^\r\n]*?\]\s*\"))((?>[^()]+)|(?2))*?\))(?!(\s*\"\s*\;|\s*\'\s*\;|\s*\/|\s*\)|\s*\"|[^\n\r]*?\"\s*\+\s*(\n|\r)?\s*\"))/", "$1 /*Yu*/ $2 /*Zu*/ ", $buffer);

// this one is  SINGLE parentheticals pair.
//     $buffer = preg_replace("/([^\(\/\"\']\s*)((\()(?=([^\n\r\)]*?[\'\"]))(?!(function|\)|[^\r\n]*?\"\s*\+[^\r\n]*?\+\s*\"|[^\r\n]*?\"\s*\<[^\r\n]*?\>\s*\"|[^\r\n]*?\"\s*\\\\\s*\"|[^\r\n]*?\"\s*\[[^\r\n]*?\]\s*\"))([^()]*?\)))(?!(\s*\"\s*\;|\s*\'\s*\;|\s*\/|\s*\)|\s*\"|[^\n\r]*?\"\s*\+\s*(\n|\r)?\s*\"))/", "$1 /*Yu*/ $2 /*Zu*/ ", $buffer); 

// (slash slash) 1 sentence! Protect with: qDdXX
do {$buffer = preg_replace('/(\/\*Yu\*\/)([^\r\n]*?)(\/)(\/)(?=([^\r\n]*?\/\*Zu\*\/))/', '$1$2qDdXX', $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(\/\*Yu\*\/)([^\n\r\'\"]*?[\"\'])([^\n\r\)]*?)(\/\*)([^\n\r\'\"\)]*?[\"\'])([^\n\r]*?\/\*Zu\*\/)/", "$1$2$3pDdYX$5$6", $buffer, 1, $count);} while ($count);
do {$buffer = preg_replace("/(\/\*Yu\*\/)([^\n\r\'\"]*?[\"\'])([^\n\r\)]*?)(\*\/)([^\n\r\'\"\)]*?[\"\'])([^\n\r]*?\/\*Zu\*\/)/", "$1$2$3ODdPK$5$6", $buffer, 1, $count);} while ($count);

//---------------------------------------------------------------------------------------------------------
// (slash slash) 2 sentences! Protect ' and "
do {$buffer = preg_replace("/(=|\+|\(|[a-z]|\,)(\s*)(\")([^\r\n\;\/\'\)\,\]\}\*]*?)(\/)(\/)([^\r\n\;\"\*]*?)(\")/", "$1$2$3$4qDdXX$7$8", $buffer, 1, $count);} while ($count);
do {$buffer = preg_replace("/(=|\+|\(|[a-z]|\,)(\s*)(\')([^\r\n\;\/\'\)\,\]\}\*]*?)(\/)(\/)([^\r\n\*\;\']*?)(\')/", "$1$2$3$4qDdXX$7$8", $buffer, 1, $count);} while ($count); 
// (slash slash) 2 sentences! Protect slash slash between ' and "
do {$buffer = preg_replace("/(\"[^\r\n\;]*?)(\/)(\/)([^\r\n\"\;]*?([\"]\s*(\;|\)|\,)))/", "$1qDdXX$4", $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(\'[^\r\n\;]*?)(\/)(\/)([^\r\n\'\;]*?([\']\s*(\;|\)|\,)))/", "$1qDdXX$4", $buffer, 1, $count);} while ($count); 
//---------------------------------------------------------------------------------------------------------
// Remove all slar slash achter \n
$buffer = preg_replace("/([\n\r])([^\n\r\*\,\"\']*?)(?=([^\*\,\:\;a-zA-Z\"]*?))(\/)(\/)+(?=([^\n\r]*?\*\/))([^\n\r]*?(\*\/)).*/", "$1$4$5 $8", $buffer); 
do {$buffer = preg_replace("/([\r\n]\s*)((\/\*(?!(\*\/)))([^\r\n]+?)(\*\/))(?!([^\n\r\/]*?(\/)(\/)+\*))/", "$1$3$6", $buffer, 1, $count);} while ($count);
$buffer = preg_replace("/([\n\r]\/)(\/)+([^\n\r]*?)(\*\/)([^\n\r]*?(\*\/))(?!([^\n\r]*?(\*\/)|[^\n\r]*?(\/\*))).*/", "$1/ $4", $buffer);  
do {$buffer = preg_replace("/([\n\r]\s*\/\*\*\/)([^\n\r=]*?\/\*[^\n\r]*?\*\/)(?=([\n\r]|\/\/))/", "$1", $buffer, 1, $count);} while ($count); 
$buffer = preg_replace("/([\n\r]\s*\/\*\*\/)([^\n\r=]*?)(\/\/.*)/", "$1$2", $buffer); 
// Remove all slash slash achter = '...'; //......
do {$buffer = preg_replace("/(\=\s*)(?=([^\r\n\'\"]*?\'[^\n\r\']*?\'))([^\n\r;]*?[;]\s*)(\/\/[^\r\n][^\r\n]*)[\n\r]/", "$1$3", $buffer, 1, $count);} while ($count);
// protect slash slash '...abc//...abc'!
do {$buffer = preg_replace("/(\=)(\s*\')([^\r\n\'\"]*?)(\/)(\/)([^\r\n]*?[\'])/", "$1$2$3qDdXX$6", $buffer, 1, $count);} while ($count);
//(slash star) or (star slash) : no dubble senteces here! Protect with: pDdYX and ODdPK
do {$buffer = preg_replace("/(\"[^\r\n\;\,\"]*?)(\/)(\*)(?!([YZ]u\*\/))([^\r\n;\,\"]*?)(\")/", "$1pDdYX$5$6", $buffer, 1, $count);} while ($count);   // open
do {$buffer = preg_replace("/([^\"]\"[^\r\n\;\/\,\"]*?)(\s*)(\*)(\/)([^\r\n;\,\"=]*?)(\")/", "$1$2ODdPK$5$6", $buffer, 1, $count);} while ($count); // close
do {$buffer = preg_replace("/(\'[^\r\n\;\,\']*?)(\/)(\*)(?!([YZ]u\*\/))([^\r\n;\,\']*?)(\')/", "$1pDdYX$5$6", $buffer, 1, $count);} while ($count);   // open
do {$buffer = preg_replace("/(\'[^\r\n\;\/\,\']*?)(\s*)(\*)(\/)([^\r\n;\,\']*?)(\')/", "$1$2ODdPK$5$6", $buffer, 1, $count);} while ($count); // close
// protect star slash '...abc*/...abc'!
do {$buffer = preg_replace("/(\'[^\r\n\;\,\']*?)(\*)(\/)([^\r\n;\,\']*?)(\')(?!([^\n\r\+]*?[\']))/", "$1ODdPK$4$5", $buffer, 1, $count);} while ($count); 
// protect star slash '...abc*/...abc'!
do {$buffer = preg_replace("/(\"[^\r\n\;\,\"]*?)(\*)(\/)([^\r\n;\,\"]*?)(\")(?!([^\n\r\+]*?[\"]))/", "$1ODdPK$4$5", $buffer, 1, $count);} while ($count);
 //---------------------------------------------------------------------------------------------------------
//// \n protect
do {$buffer = preg_replace("/(=\s*\"[^\n\r\"]*?)(\/\/)(?=([^\n\r]*?\"\s*;))/", "$1qDdXX", $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(=\s*\"[^\n\r\"]*?)(\/\*)(?!([YZ]u\*\/))(?=([^\n\r]*?\"\s*;))/", "$1pDdYX", $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(=\s*\"[^\n\r\"]*?)(\*\/)(?=([^\n\r]*?\"\s*;))/", "$1ODdPK", $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(=\s*\'[^\n\r\']*?)(\/\/)(?=([^\n\r]*?\'\s*;))/", "$1qDdXX", $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(=\s*\'[^\n\r\']*?)(\/\*)(?!([YZ]u\*\/))(?=([^\n\r]*?\'\s*;))/", "$1pDdYX", $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(=\s*\'[^\n\r\']*?)(\*\/)(?=([^\n\r]*?\'\s*;))/", "$1ODdPK", $buffer, 1, $count);} while ($count); 
//---------------------------------------------------------------------------------------------------------
// (Slash Slash) alle = " // " and = ' // ' replace by! qDdXX
do {$buffer = preg_replace("/(\=|\()(\s*\")([^\r\n\'\"]*?[\'][^\r\n\'\"]*?)(\/)(\/)([^\r\n\'\"]*?[\'])(\s*\'[^\r\n\'\"]*?)(\/\/|qDdXX)?([^\r\n\'\"]*?[\'][^\r\n\'\"]*?[\"])(?!(\'\)|\s*[\)]?\s*\+|\'))/", "$1$2$3qDdXX$6$7qDdXX$9$10", $buffer, 1, $count);} while ($count); 
do {$buffer = preg_replace("/(\=|\()(\s*\')([^\r\n\'\"]*?[\"][^\r\n\'\"]*?)(\/)(\/)([^\r\n\'\"]*?[\"])(\s*\"[^\r\n\'\"]*?)(\/\/|qDdXX)?([^\r\n\'\"]*?[\"][^\r\n\'\"]*?[\'])(?!(\'\)|\s*[\)]?\s*\+|\'))/", "$1$2$3qDdXX$6$7qDdXX$9$10", $buffer, 1, $count);} while ($count); 
//---------------------------------------------------------------------------------------------------------
// (slash slash) Remove all also , or + not followed by */ and newline
$buffer = preg_replace("/([^\*])(\*|[\r\n]|[^\\\\]\'|[^\\\\]\"|\,|\+|\{|;|\(|\)|\[|\]|\{|\}|\?|[^p|s]:|\&|\%|[^\\\\][a-m-o-u-s-zA-Z]|\||-|=|[0-9])(\s*)(?!([^=\\\\\&\/\"\'\^\*:]))(\/)(\/)+(?!([\r\n\*\+\"]*?([^\r\n]*?\*\/|[^\r\n]*?\"\s*\+|([^\r\n]*?=\";)))).*/", "$1$2$3", $buffer);
// (slash slash star slash) Remove everhing behinde it not followed by */ or new line
$buffer = preg_replace("/(\/\/\*\/)(?!([\r\n\*\+\"]*?([^\r\n]*?\*\/|[^\r\n]*?\"\s*\+|([^\r\n]*?=\";)))).*/", "", $buffer);
// Remove almost all star comments except colon/**/
$buffer = preg_replace("/(?!([^\n\r]*?[\'\"]))(\s*<!--.*-->)(?!(<\/div>))[^\n\r]*?.*/","$2$4", $buffer);
$buffer = preg_replace("/([\n\r][^\n\r\*\,\"\']*?)(?=([^\*\,\:\;a-zA-Z\"]*?))(\/)(\/)+(?!([\r\n\*\+\"]*?([^\r\n]*?\*\/|[^\r\n]*?\"\s*\+|([^\r\n]*?=\";)))).*/", "$1", $buffer);
$buffer = preg_replace("/(?!([^\n\r]*?[\'\"]))(\s*<!--.*-->)(?!(<\/div>))[^\n\r]*?(\*\/)?.*/","", $buffer);
$buffer = preg_replace("/(<!--.*?-->)(?=(\s*<\/div>))/","", $buffer);
//---------------------------------------------------------------------------------------------------------
// Restore all
$buffer = preg_replace("/qDdXX/", "//", $buffer);  // Restore //
$buffer = preg_replace("/pDdYX/", "/*", $buffer);   // Restore 
$buffer = preg_replace("/ODdPK/", "*/", $buffer);   // Restore 
$buffer = preg_replace("/RDdPK/", "*/*", $buffer);   // Restore 
$buffer = preg_replace("/TDdXX/", "//*", $buffer);   // Restore */
$buffer = preg_replace('/WQerT/', '\\\\r\\\\n" +', $buffer);   // Restore \r\n" + 
$buffer = preg_replace('/YQerT/', '//*/\\\\r\\\\n" +', $buffer);   // Restore \r\n" + 
$buffer = preg_replace('/AQerT/', '\\\\n', $buffer);   // Restore \n" 
$buffer = preg_replace('/BQerT/', '\\\\r', $buffer);   // Restore \r"
$buffer = preg_replace("/ZUQQ/", "/ /", $buffer);
$buffer = preg_replace('/\s\/\*Zu\*\/\s/', '', $buffer);   // Restore \n"
$buffer = preg_replace('/\s\/\*Yu\*\/\s/', '', $buffer);   // Restore \n"
////---------------------------------------------------------------------------------------------------------
//// Remove all markings!
$buffer = preg_replace('/(AwTc)/', '', $buffer);  // Start most Regex!
$buffer = preg_replace('/(CwRc)/', '', $buffer);  // End Most regex!
// all \s and [\n\r] repair like they where!
$buffer = preg_replace("/([a-zA-Z0-9]\s?)\s*[\n\r]+(\s*[\)\,&]\s?)(\s*[\r\n]+\s*[\{])/", "$1$2$3", $buffer); 
$buffer = preg_replace("/([a-zA-Z0-9\(]\s?)\s*[\n\r]+(\s*[;\)\,&\+\-a-zA-Z0-9]\s?)(\s*[\{;a-zA-Z0-9\,&\n\r])/", "$1$2$3", $buffer); 
$buffer = preg_replace("/(\(\s?)\s*[\n\r]+(\s*function)/", "$1$2", $buffer);
$buffer = preg_replace("/(=\s*\[[a-zA-Z0-9]\s?)\s*([\r\n]+)/", "$1", $buffer); 
//-----------------------------------------------
$buffer = preg_replace("/([^\*\/\'\"]\s*)(\/\/\s*\*\/)/", "$1", $buffer);
//// Remove all /**/// .... Remove expept /**/ and followed by */ till newline!
$buffer = preg_replace("/(\/\*\*\/)(\/\/(?!([^\n\r]*?\*\/)).*)/", "$1", $buffer);
$buffer = preg_replace("/(\;\/\*\*\/)(?!([^\n\r]*?\*\/)).*/", "", $buffer);
$buffer = preg_replace("/(\/\/\\\\\*[^\n\r\"\'\/]*?[\n\r])/", "\r\n", $buffer);
$buffer = preg_replace("/([\r\n]\s*)(\/\*[^\r\n]*?\*\/(?!([^\r\n]*?\"\s*\+)))/", "$1", $buffer);
//Remove colon /**/
$buffer = preg_replace("/\/\*\*\/\s/", " ", $buffer);
$buffer = preg_replace("/(\=\s*)(?=([^\r\n\'\"]*?\'[^\n\r\'\"]*?\'))([^\n\r\/]*?)(\/\/[^\r\n\"\'][^\r\n]*[\'\"])(\/\*\*\/)[\n\r]/", "$1$3$4\n", $buffer);
$buffer = preg_replace("/(\=\s*)(?=([^\r\n\'\"]*?\"[^\n\r\'\"]*?\"))([^\n\r\/]*?)(\/\/[^\r\n\"\'][^\r\n]*[\'\"])(\/\*\*\/)[\n\r]/", "$1$3$4\n", $buffer);
//Remove colon //
$buffer = preg_replace("/([^\'\"ps\s]\s*)(\:[^\r\n\'\"\[\]]*?\'[^\n\r\'\"]*?\')([^\n\r\/a-zA-Z0-9]*?)(\/\/)[^\r\n\/\'][^\r\n]*/", "$1$2", $buffer);
$buffer = preg_replace("/([^\'\"ps\s]\s*)(\:[^\r\n\'\"\[\]]*?\"[^\n\r\'\"]*?\")([^\n\r\/a-zA-Z0-9]*?)(\/\/)[^\r\n\/\"][^\r\n]*/", "$1$2", $buffer);
$buffer = preg_replace("/(\"[^\n\r\'\"\+]*?\")([^\n\r\/a-zA-Z0-9]*?)(\/\/)(?!(\*|[^\r\n]*?[\\\\n\\\\r]+\s*\"\s*\+|[^\r\n]*?[\\\\n\\\\r]+\s*\'\s*\+))[^\r\n\/\"][^\r\n]*/", "$1$2", $buffer);
//Remove all after ; slah slah+
$buffer = preg_replace("/(;\s*)\/\/(?!([^\n\r]*?\"\s*;)).*/", "$1 \n", $buffer);
//Remove: ) /* non-empty*//*xxx*/)
$buffer = preg_replace('/([\n\r][^\n\r\"]*?)([^\/\"\'\*\>])\/\*(?!(\*\/))[^\n\r\"]*?[^@]\*\//', "$1$2", $buffer);
//Remove // vooraf gegaan door: || | ? | , //
$buffer = preg_replace("/(\|\||[\?]|\,)(\s*)\/\/(?!([^\n\r]*?\*\/|\"|\')).*/", "$1$2", $buffer);
//Remove: [\n\r] after: [ \|\[\;\,\:\=\-\{\}\]\[\?\)\( ]
$buffer = preg_replace("/([\|\[\;\,\:\=\-\{\}\]\[\?\)\(])\s*[\n\r]\s*[\n\r](\s*[\n\r])+/", "$1\n", $buffer);
////---------------------------------------------------------------------------------------------------------
//END Remove comments.    //START Remove all whitespaces    
$buffer = preg_replace('/(--\s+\>)/', 'HwRc', $buffer);  // protect space between: -- >
//-------------------------------------------------------- 
$buffer = preg_replace('/\s+/', ' ', $buffer);
$buffer = preg_replace('/\s*(?:(?=[=\-\+\|%&\*\)\[\]\{\};:\,\.\<\>\!\@\#\^`~]))/', '', $buffer);
$buffer = preg_replace('/(?:(?<=[=\-\+\|%&\*\)\[\]\{\};:\,\.\<\>\?\!\@\#\^`~]))\s*/', '', $buffer);
$buffer = preg_replace('/([^a-zA-Z0-9\s\-=+\|!@#$%^&*()`~\[\]{};:\'",<.>\/?])\s+([^a-zA-Z0-9\s\-=+\|!@#$%^&*()`~\[\]{};:\'",<.>\/?])/', '$1$2', $buffer);
//-------------------------------------------------------
$buffer = preg_replace('/(HwRc)/', '-- >', $buffer);  // Repair space between: -- >
//END Remove all whitespaces

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???

I am off coarse not a programmer just wanted to
make the plugin work like i wanted it to....
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..
Because I do have a big handycap I do not now any Javascript syntax
so I do not know what to expect I just tested it on examples I could find!

Updated needs no more testing 8000.000 lines work fine now.!

戒ㄋ 2024-10-13 23:20:22

我遇到了一个场景,我通过 xhr 通过网络加载 javascript 代码,并且需要清理代码以进行评估。我正在使用这段代码:

var cleanCode = function cleanCode(input) {
  // Remove special characters:
  return input.replace(/\/\/[^"'].*?\n/g, '') /* //comments, but ignore a "//" if there is a quote after it */
              .replace(/\n/g, '')             /* Leftover newlines */
              .replace(/\r/g, '');            /* Carriage returns  */
};

我使用这个进行了测试,唯一的障碍是这样的注释:

//'key': 'value'

所以我必须将该代码更改为内联注释:

/*'key': 'value'*/ //LEAVE as inline comment, please

此代码不会更改:

url += '//' + ...

请随意编辑/建议编辑使其更具可读性。

I hit a scenario where I load javascript code over the network via xhr, and need to clean the code for eval'ing. I'm using this code:

var cleanCode = function cleanCode(input) {
  // Remove special characters:
  return input.replace(/\/\/[^"'].*?\n/g, '') /* //comments, but ignore a "//" if there is a quote after it */
              .replace(/\n/g, '')             /* Leftover newlines */
              .replace(/\r/g, '');            /* Carriage returns  */
};

I tested using this and the only snag I had was a comment like:

//'key': 'value'

So I had to change that code to the inline comment:

/*'key': 'value'*/ //LEAVE as inline comment, please

This code doesn't get altered:

url += '//' + ...

Feel free to edit/suggest edits to make this more readable.

贪了杯 2024-10-13 23:20:22

JS 中删除注释的模式

$pattern = '/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/';

CSS 中删除注释的模式

$pattern = '!/\*[^*]*\*+([^/][^*]*\*+)*/!'; 

$str = preg_replace($pattern, '', $str);

我希望上面的内容可以帮助某人。

Pattern for remove comments in JS

$pattern = '/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/';

Pattern for remove comments in CSS

$pattern = '!/\*[^*]*\*+([^/][^*]*\*+)*/!'; 

$str = preg_replace($pattern, '', $str);

I hope above should help someone..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文