代码之家  ›  专栏  ›  技术社区  ›  JarsOfJam-Scheduler

在字符串中,将每个具有某些属性的HTML元素添加到具有任意属性的任意值的新任意元素中

  •  1
  • JarsOfJam-Scheduler  · 技术社区  · 6 年前

    我有一个包含以下HTML代码的字符串:

    abc <h1><div style="color: rgb(190, 6, 6);">ABC <span style="color: rgb(0, 255, 6);">DEF</span></div></h1>
    

    我的程序必须检测到 color 已被使用,因此它会自动添加元素( div span )每一个都变成一个新元素 font . 即使有几个元素每个都包含 颜色 规则,在字符串中,其他HTML元素添加在左侧或右侧。

    我怎么能这样做?

    上述代码将变成:

    abc <h1><font color="rgb(190, 6, 6)"><div style="color: rgb(190, 6, 6);">ABC <font color="rgb(0, 255, 6);"><span style="color: rgb(0, 255, 6);">DEF</span></font></div></font></h1>
    
    0 回复  |  直到 6 年前
        1
  •  1
  •   Hari Prasad    6 年前

    使用此正则表达式搜索和替换

    pattern = "(<div[\s]+style=")(color:[\s]+rgb\([\d]+, [\d]+, [\d]+\))"
    
    replace by "<font color=\"$2\">$1\"$2\"