【问题标题】:How to remove some css properties using regular expression?如何使用正则表达式删除一些 CSS 属性?
【发布时间】:2011-12-07 14:21:30
【问题描述】:
"outline-style: none; margin: 0px; padding: 2px; background-color: #eff0f8; color: #3b3a39; font-family: Georgia,'Times New Roman',Times,serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: center; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border: 1px solid #ebebeb; float: left;"

我有这个作为内联 css。我想用正则表达式替换所有以“背景”和“字体”开头的属性。在 inline css 中,最后一个属性可能没有分号作为结尾

我将此代码用作 django 过滤器,以使用漂亮的汤从服务器端删除这些属性

def html_remove_attrs(value):
    soup = BeautifulSoup(value)
    print "hi"
    for tag in soup.findAll(True,{'style': re.compile(r'')}): 
        #tag.attrs = None
        #for attr in tag.attrs:
        #    if "class" in attr:
        #        tag.attrs.remove(attr)
        #    if "style" in attr:
        #        tag.attrs.remove(attr)
        for attr in tag.attrs:
            if "style" in attr:
                #remove the background and font properties 

    return soup

【问题讨论】:

  • 您是在它上线之前还是在它到达客户端(javascript?)时这样做?
  • 我必须从服务器端解析它..
  • 您可能应该重新考虑使用“内联 css”来支持可重用的类。
  • 我得到的内容是 TinyMCE 粘贴的 HTML,由其他一些网站的用户发布。我必须替换元素的 font* 和 background* 属性以使内容与我的 Web 主题兼容

标签: css regex


【解决方案1】:

我不知道你的编程环境的细节,但你要求一个正则表达式。此正则表达式将查找属性键(加上冒号和任何空格)作为第 1 组 ($1) 并将属性值作为第 2 组 ($2):

 ((?:background|font)(?:[^:]+):(?:\\s*))([^;]+)

表达式不会删除属性值。它找到了它们。如何删除它们取决于您的编程环境(语言/库)。

但基本上,您将进行全局查找/替换,将整个结果替换为 $1

例如,使用 Java 你可以做到这一点

public static void main(String[] args) throws Exception {

    String[] lines = {
        "outline-style: none; margin: 0px; padding: 2px; background-color: #eff0f8; color: #3b3a39; font-family: Georgia,'Times New Roman',Times,serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: center; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border: 1px solid #ebebeb; float: left;",
        "outline-style: none; margin: 0px; padding: 2px; background-color: #eff0f8; color: #3b3a39; font-family: Georgia,'Times New Roman',Times,serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: center; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border: 1px solid #ebebeb; float: left",
        "background-color: #eff0f8;",
        "background-color: #eff0f8",
    };

    String regex = "((?:background|font)(?:[^:]+):(?:\\s*))([^;]+)";

    Pattern p = Pattern.compile(regex);

    for (String s: lines) {
        StringBuffer sb = new StringBuffer();
        Matcher m = p.matcher(s);
        while (m.find()) {

            // capturing group(2) for debug purpose only
            // just to get it's length so we can fill that with '-' 
            // to assist comparison of before and after
            String text = m.group(2);
            text = text.replaceAll(".", "-");
            m.appendReplacement(sb, "$1"+text);

            // for non-debug mode, just use this instead
            // m.appendReplacement(sb, "$1");
        }
        m.appendTail(sb);

        System.err.println("> " + s); // before
        System.err.println("< " +sb.toString()); // after
        System.err.println();
    }
}

【讨论】:

  • 确实是很棒的表达。谢谢你的帮助。但是当我用这个正则表达式拆分并将所有拆分的数据连接在一起时,我得到了这个 pastebin.com/n43wUw8x 。 "background*" 和 "font*" 的值不会被删除:(
  • 我已经修改了表达式并更新了答案,包括一个例子。
猜你喜欢
  • 1970-01-01
  • 2020-12-04
  • 1970-01-01
  • 1970-01-01
  • 2014-11-21
  • 2015-12-02
  • 1970-01-01
  • 2019-02-16
  • 2011-04-18
相关资源
最近更新 更多