Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
缩小:使用正则表达式从JavaScript代码中删除换行符_Javascript_Php_Regex_Minify - Fatal编程技术网

缩小:使用正则表达式从JavaScript代码中删除换行符

缩小:使用正则表达式从JavaScript代码中删除换行符,javascript,php,regex,minify,Javascript,Php,Regex,Minify,出于混淆的纯粹目的,前三行似乎很好地清除了脚本中不必要的输入 有人能告诉我1-4行的实际功能吗?从试错中我知道的唯一一件事是,如果我把第四行注释掉,网站就会工作,如果我把它留在原地,网站就会崩溃 <?php header("Content-type: text/javascript; charset=UTF-8"); ob_start("compress"); function compress($buffer) { # remove extra or unnecces

出于混淆的纯粹目的,前三行似乎很好地清除了脚本中不必要的输入

  • 有人能告诉我1-4行的实际功能吗?从试错中我知道的唯一一件事是,如果我把第四行注释掉,网站就会工作,如果我把它留在原地,网站就会崩溃

    <?php
    
    header("Content-type: text/javascript; charset=UTF-8");   
    ob_start("compress"); 
    function compress($buffer) 
    {
        # remove extra or unneccessary new line from javascript
        $buffer = preg_replace('/([;])\s+/', '$1', $buffer);
        $buffer = preg_replace('/([}])\s+(else)/', '$1else', $buffer);
        $buffer = preg_replace('/([}])\s+(var)/', '$1;var', $buffer);
        $buffer = preg_replace('/([{};])\s+(\$)/', '$1\$', $buffer);
    
        return $buffer;
    }
    
    所有四个正则表达式的剖析
    让我们试着剖析每一个正则表达式

    第一个正则表达式

    $buffer = preg_replace('/([;])\s+/', '$1', $buffer);
    
    $buffer = preg_replace('/([}])\s+(else)/', '$1else', $buffer);
    
    $buffer = preg_replace('/([}])\s+(var)/', '$1;var', $buffer);
    
    $buffer = preg_replace('/([{};])\s+(\$)/', '$1\$', $buffer);
    
    解释

    (      # beginning of the first capturing group
     [;]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters (including newlines)
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (else) # match and capture 'else'
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (var)  # match and capture 'var'
    
    (      # beginning of the first capturing group
     [{};] # match the literal character '{' or '}' or ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (      # beginning of the second capturing group
     \$    # match the literal character '$'
    )      # ending of the second capturing group
    
    上面的正则表达式删除分号后面紧跟的任何空格
    ([;])
    是一个捕获组,这意味着如果找到匹配项,它将存储到反向引用中,以便我们以后使用它。例如,如果我们的字符串是
    foo
    ,则表达式将匹配
    和空白字符。这里的替换模式是
    $1
    ,这意味着整个匹配的字符串将被替换为一个分号


    第二个正则表达式

    $buffer = preg_replace('/([;])\s+/', '$1', $buffer);
    
    $buffer = preg_replace('/([}])\s+(else)/', '$1else', $buffer);
    
    $buffer = preg_replace('/([}])\s+(var)/', '$1;var', $buffer);
    
    $buffer = preg_replace('/([{};])\s+(\$)/', '$1\$', $buffer);
    
    解释

    (      # beginning of the first capturing group
     [;]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters (including newlines)
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (else) # match and capture 'else'
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (var)  # match and capture 'var'
    
    (      # beginning of the first capturing group
     [{};] # match the literal character '{' or '}' or ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (      # beginning of the second capturing group
     \$    # match the literal character '$'
    )      # ending of the second capturing group
    
    上面的正则表达式删除了右大括号(
    }
    )和
    else
    之间的任何空格。这里的替换模式是
    $1else
    ,这意味着带空格的字符串将被第一个捕获组
    ([}])
    (仅为分号)捕获的内容替换,然后是关键字
    else
    。没什么大不了的


    第三个正则表达式

    $buffer = preg_replace('/([;])\s+/', '$1', $buffer);
    
    $buffer = preg_replace('/([}])\s+(else)/', '$1else', $buffer);
    
    $buffer = preg_replace('/([}])\s+(var)/', '$1;var', $buffer);
    
    $buffer = preg_replace('/([{};])\s+(\$)/', '$1\$', $buffer);
    
    解释

    (      # beginning of the first capturing group
     [;]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters (including newlines)
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (else) # match and capture 'else'
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (var)  # match and capture 'var'
    
    (      # beginning of the first capturing group
     [{};] # match the literal character '{' or '}' or ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (      # beginning of the second capturing group
     \$    # match the literal character '$'
    )      # ending of the second capturing group
    
    这与以前的正则表达式相同。这里唯一的区别是关键字-
    var
    而不是
    else
    。分号字符在JavaScript中是可选的。但是,如果您想在一行中编写多个语句,解释器无法知道它们是多行语句,因此a
    来终止每个语句


    第四个正则表达式

    $buffer = preg_replace('/([;])\s+/', '$1', $buffer);
    
    $buffer = preg_replace('/([}])\s+(else)/', '$1else', $buffer);
    
    $buffer = preg_replace('/([}])\s+(var)/', '$1;var', $buffer);
    
    $buffer = preg_replace('/([{};])\s+(\$)/', '$1\$', $buffer);
    
    解释

    (      # beginning of the first capturing group
     [;]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters (including newlines)
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (else) # match and capture 'else'
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (var)  # match and capture 'var'
    
    (      # beginning of the first capturing group
     [{};] # match the literal character '{' or '}' or ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (      # beginning of the second capturing group
     \$    # match the literal character '$'
    )      # ending of the second capturing group
    
    这里的替换模式是
    $1\$
    ,这意味着整个匹配的字符串将被第一个捕获组
    ([{};])
    所匹配的字符串替换,然后是一个文本
    $
    字符

    旁注 这个答案只是为了解释这四个正则表达式及其作用。表达式可以改进很多,但我不打算讨论,因为这不是正确的方法。正如Qtax在评论中指出的那样,您确实应该使用适当的JS minifier来完成此任务。你可能想去看看——它看起来很整洁

    如果你仍然不知道它是如何工作的,不要担心。一开始学习正则表达式可能很困难。我建议你使用这个网站-。这是学习正则表达式的一个相当不错的资源。如果你在找一本书,你可能想看看Jeffrey Friedl写的。

    对所有四个正则表达式的剖析 让我们试着剖析每一个正则表达式

    第一个正则表达式

    $buffer = preg_replace('/([;])\s+/', '$1', $buffer);
    
    $buffer = preg_replace('/([}])\s+(else)/', '$1else', $buffer);
    
    $buffer = preg_replace('/([}])\s+(var)/', '$1;var', $buffer);
    
    $buffer = preg_replace('/([{};])\s+(\$)/', '$1\$', $buffer);
    
    解释

    (      # beginning of the first capturing group
     [;]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters (including newlines)
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (else) # match and capture 'else'
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (var)  # match and capture 'var'
    
    (      # beginning of the first capturing group
     [{};] # match the literal character '{' or '}' or ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (      # beginning of the second capturing group
     \$    # match the literal character '$'
    )      # ending of the second capturing group
    
    上面的正则表达式删除分号后面紧跟的任何空格
    ([;])
    是一个捕获组,这意味着如果找到匹配项,它将存储到反向引用中,以便我们以后使用它。例如,如果我们的字符串是
    foo
    ,则表达式将匹配
    和空白字符。这里的替换模式是
    $1
    ,这意味着整个匹配的字符串将被替换为一个分号


    第二个正则表达式

    $buffer = preg_replace('/([;])\s+/', '$1', $buffer);
    
    $buffer = preg_replace('/([}])\s+(else)/', '$1else', $buffer);
    
    $buffer = preg_replace('/([}])\s+(var)/', '$1;var', $buffer);
    
    $buffer = preg_replace('/([{};])\s+(\$)/', '$1\$', $buffer);
    
    解释

    (      # beginning of the first capturing group
     [;]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters (including newlines)
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (else) # match and capture 'else'
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (var)  # match and capture 'var'
    
    (      # beginning of the first capturing group
     [{};] # match the literal character '{' or '}' or ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (      # beginning of the second capturing group
     \$    # match the literal character '$'
    )      # ending of the second capturing group
    
    上面的正则表达式删除了右大括号(
    }
    )和
    else
    之间的任何空格。这里的替换模式是
    $1else
    ,这意味着带空格的字符串将被第一个捕获组
    ([}])
    (仅为分号)捕获的内容替换,然后是关键字
    else
    。没什么大不了的


    第三个正则表达式

    $buffer = preg_replace('/([;])\s+/', '$1', $buffer);
    
    $buffer = preg_replace('/([}])\s+(else)/', '$1else', $buffer);
    
    $buffer = preg_replace('/([}])\s+(var)/', '$1;var', $buffer);
    
    $buffer = preg_replace('/([{};])\s+(\$)/', '$1\$', $buffer);
    
    解释

    (      # beginning of the first capturing group
     [;]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters (including newlines)
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (else) # match and capture 'else'
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (var)  # match and capture 'var'
    
    (      # beginning of the first capturing group
     [{};] # match the literal character '{' or '}' or ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (      # beginning of the second capturing group
     \$    # match the literal character '$'
    )      # ending of the second capturing group
    
    这与以前的正则表达式相同。这里唯一的区别是关键字-
    var
    而不是
    else
    。分号字符在JavaScript中是可选的。但是,如果您想在一行中编写多个语句,解释器无法知道它们是多行语句,因此a
    来终止每个语句


    第四个正则表达式

    $buffer = preg_replace('/([;])\s+/', '$1', $buffer);
    
    $buffer = preg_replace('/([}])\s+(else)/', '$1else', $buffer);
    
    $buffer = preg_replace('/([}])\s+(var)/', '$1;var', $buffer);
    
    $buffer = preg_replace('/([{};])\s+(\$)/', '$1\$', $buffer);
    
    解释

    (      # beginning of the first capturing group
     [;]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters (including newlines)
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (else) # match and capture 'else'
    
    (      # beginning of the first capturing group
     [}]   # match the literal character ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (var)  # match and capture 'var'
    
    (      # beginning of the first capturing group
     [{};] # match the literal character '{' or '}' or ';'
    )      # ending of the first capturing group
    \s+    # one or more whitespace characters
    (      # beginning of the second capturing group
     \$    # match the literal character '$'
    )      # ending of the second capturing group
    
    这里的替换模式是
    $1\$
    ,这意味着整个匹配的字符串将被第一个捕获组
    ([{};])
    所匹配的字符串替换,然后是一个文本
    $
    字符

    旁注 这个答案只是为了解释这四个正则表达式及其作用。表达式可以改进很多,但我不打算讨论,因为这不是正确的方法。正如Qtax在评论中指出的那样,您确实应该使用适当的JS minifier来完成此任务。你可能想去看看——它看起来很整洁


    如果你仍然不知道它是如何工作的,不要担心。一开始学习正则表达式可能很困难。我建议你使用这个网站-。这是学习正则表达式的一个相当不错的资源。如果你正在找一本书,你可能想看看Jeffrey Friedl写的。

    回答2:是的,使用合适的JS迷你版。谷歌是我上次检查的最好的一个。回答2:是的,使用一个合适的JS浏览器。谷歌是我最后检查过的最好的一个。