正则表达式缩小内联JS

时间:2014-04-01 16:56:13

标签: javascript php regex minify

使用ob_start(PHP)我使用从StackOverflow复制的正则表达式缩小我的HTML我相信:

$regex = '%# Collapse whitespace everywhere but in blacklisted elements.
    (?>             # Match all whitespans other than single space.
      [^\S ]\s*     # Either one [\t\r\n\f\v] and zero or more ws,
    | \s{2,}        # or two or more consecutive-any-whitespace.
    ) # Note: The remaining regex consumes no text at all...
    (?=             # Ensure we are not in a blacklist tag.
      [^<]*+        # Either zero or more non-"<" {normal*}
      (?:           # Begin {(special normal*)*} construct
        <           # or a < starting a non-blacklist tag.
        (?!/?(?:textarea|pre|script)\b)
        [^<]*+      # more non-"<" {normal*}
      )*+           # Finish "unrolling-the-loop"
      (?:           # Begin alternation group.
        <           # Either a blacklist start tag.
        (?>textarea|pre|script)\b
      | \z          # or end of file.
      )             # End alternation group.
    )  # If we made it here, we are not in a blacklist tag.
    %Six';
$text = preg_replace($regex, ' ', $text);

这很好用,除了我想从HTML文件中的内联JS中删除一些空格。它不一定是最先进的,只是JS的一些基本缩小。我将如何为此编写正则表达式?

我尝试使用前瞻和后视,但是后视必须是固定长度,因此造成了一些麻烦。我也很难理解上面的正则表达式......

有人有一个基本的例子,我如何在脚本标签中匹配不必要的空格?

0 个答案:

没有答案