如何使用 PHP 去除 JS 注释?
此问题更新于:2013 年 11 月 4 日,回答者:Alexander Yancharuk
但是现在有一个问题。新代码:id = id.replace(/\//g,'');
这是我的例子:
<?php
$output = "
//remove comment
this1 //remove comment
this2 /* remove comment */
this3 /* remove
comment */
this4 /* * * remove
* * * *
comment * * */
this5 http://removecomment.com
id = id.replace(/\//g,''); //do not remove the regex //
";
$output = preg_replace( "/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:)\/\/.*))/", "", $output ); //Yancharuk's code/regex
// "/(?<!\:)\/\/(.*)\\n/ = my oldest code
echo nl2br($output);
?>
我的问题;
- this1 行有问题;
- //comments 有效,但我无法创建代码来删除/* comment */或带换行符的评论
这是最近的输出:
这1
这2
这3
这4
这5 http://removecomment.com
id = id.replace(/\
最佳答案
试试这个:
$output = "
//remove comment
this1 //remove comment
this2 /* remove comment */
this3 /* remove
comment */
this4 /* * * remove
* * * *
comment * * */
this5 http://removecomment.com
id = id.replace(/\//g,''); //do not remove the regex //
HTTP+'//www.googleadservices.com/pagead/conversion'
";
$pattern = '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\')\/\/.*))/';
$output = preg_replace($pattern, '', $output);
echo nl2br($output);
codepad.org 上的结果.
关于javascript - 如何使用 PHP 去除 JS 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19509863/