为什么 JS 中的这些缩小注释不会破坏代码?

Why these minified comments in JS don't break the code?

我有一个网页,在将它发送到浏览器之前,我使用 PHP 删除了多个空格、制表符和每个新行。

                    $output = preg_replace('/\n/', '', $output);
                    $output = preg_replace('/( |    ){2,}/', ' ', $output);

现在,重新查看代码,我注意到在 JS 代码中我留下了一些 //comments...

查看源码可以看到:

[...]lback, args) {     //step == -1 -> loop    if (step > n) {   clearTimeout(tim[...]

为什么这个评论 (//step == -1 -> loop) 没有破解密码?!

为什么如果我打开检查器,我会看到 "order" 中的元素就像 "new lines" 中的元素?

我认为是"new line"的问题,应该有一些其他的字符告诉浏览器该行的结束和开始,是吗?

如果是,我如何才能确定将代码移动到一行,如何处理 JS 一行注释?

谢谢

您的文件可能使用了 DOS 类型的换行符(回车符-return + 换行符,\r\n)。通过替换换行符(\n),carriage-return(\r)保留在文件中,这是Apple操作系统使用的换行符

这就是你所看到的

[...]lback, args) {     //step == -1 -> loop    if (step > n) {   clearTimeout(tim[...]

实际上是

[...]lback, args) {\r     //step == -1 -> loop\r    if (step > n) {\r   clear[...]

如果您也想删除评论(对于最小化是有意义的,但可能会破坏例如 Internet Explorer 条件评论),您可以通过

替换它们
$output = preg_replace("/\/\/[^\r\n]*", "", $output); // single-line comments
$output = preg_replace("/\/*[\s\S]*?*\/", "", $output); // multi-line comments

你应该在替换换行符之前这样做。