javascript - 如何使用 PHP 去除 JS 注释?

标签 javascript php

如何使用 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);
?>

我的问题;

  1. this1 行有问题;
  2. //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/

相关文章:

php - 如何使用 PHP 将文本文件的内容存储在数据库中

javascript - 尽管使用 addEventListener 在父级上设置了单击处理程序,但子级 div 触发操作上的单击事件(事件冒泡被禁用))

javascript - 我怎样才能在 javascript 中停止鼠标事件?

php - php restore_error_handler无法正常工作

php - 如何从 HTML 表单中获取 PHP 值?

php - 问两次会带来不好的表现吗?

php - 多步骤表单 JavaScript 验证以防止继续执行其他步骤

javascript - 获取用户在draftjs中输入的文本

javascript - Jquery Click 事件仅触发一次

javascript - 在Sencha Touch中获取容器在 "initialize"上的记录