Using regex to match the start and end of a media query, but not the content inbetween

匆匆过客 提交于 2019-12-13 17:33:09

问题


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

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.

Thanks.


回答1:


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

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


来源:https://stackoverflow.com/questions/24183531/using-regex-to-match-the-start-and-end-of-a-media-query-but-not-the-content-inb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!