使用正则表达式匹配媒体查询的开始和结束,但不匹配两者之间的内容 [英] Using regex to match the start and end of a media query, but not the content inbetween

查看:98
本文介绍了使用正则表达式匹配媒体查询的开始和结束,但不匹配两者之间的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用htmlcompressor( https://code.google.com/p/htmlcompressor/#For_Non-Java_Projects )以最小化HTML内容(HTML电子邮件模板)以及文档顶部的内联CSS.我正在Windows的命令行上运行它:

I'm using the htmlcompressor (https://code.google.com/p/htmlcompressor/#For_Non-Java_Projects) to minify HTML content (HTML email templates) as well as inline CSS within the head section of the document. I'm running this via Command Line on Windows:

java -jar /path/to/htmlcompressor --preserve-comments --preserve-line-breaks --compress-css --remove-surrounding-spaces min -p /path/to/minify-preserve.txt --type html original-html -o minified-output.html

在本文档中,有两个媒体查询,我需要它们来防止htmlcompressor由于语法问题而被修改.我环顾四周,发现此正则表达式规则:

Within this document there are two media queries which I need to prevent the htmlcompressor from modifying due to syntax issues. I was looking around and found this regex rule:

@media[^{]+\{([\s\S]+?})\s*}

但是,它匹配媒体查询及其之间的内容,理想情况下,我需要它仅匹配起始块和结尾括号,因此它在这些区域中保留了有效语法的空间.

It however matches the media queries and the contents between, ideally, I need it to match only the starting block and end brackets, so its maintains spaces in these areas for valid syntax.

使用正则表达式可以实现吗?根据文档,我仅限于正则表达式作为创建保留规则的唯一方法.

Would this be achievable with regex? I am limited to regex only as its the only method of creating preservation rules, as per the documentation.

谢谢.

推荐答案

您的常规表达式几乎是正确的.但是在关闭花括号之前缺少(反斜杠)

You'r regular expresion is almost right. but (backward slash) is missing before closing curly brace

@media[^{]+\{([\s\S]+?\})\s*\}

这篇关于使用正则表达式匹配媒体查询的开始和结束,但不匹配两者之间的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆